CLIM on Android

This post shows a way to use the CLIM library inside an Android app.

The problem

We’d like to use a simple activity with an EditText (maybe multiline) as output and another EditText as input. Since the CLIM’s engine use Standard input/output streams as default, we need to implement our custom ones.

The Layout

The layout of our activity will consists on:

  • Multiline EditText etout as output
  • EditText etin as input
  • Button btnok to commit the input (equivalent to ENTER key)

The activity_main.xml :

CLIM dependency

First things first: let’s add dependancy to CLIM in the gradle project-level build.gradle :

then the app-level build.gradle:

The Activity

Inside our activity we will prepare our CLIM engine:

For an implementation with sense, please have a look at the Demo.java class.
We will see the body of prepareStreams(Engine engine) in the next section.

The Streams

Now, we need to implement our custom input and output streams to be linked to our EditTexts etin and etout respectively.

Let’s see the content of prepareStreams():

As you can see, the implementation of the OutputStream is quite simple, since we will redirect the input string to the output EditText (in the UI thread, of course).

The TextInputStream needs some extra considerations:

You can notice that the TextInputStream take the engine as the main subscriber and saves to lastContent the inputted text. The access to lastContent is managed by two synchronized methods (getLastContent() and setLastContent(…)). Finally, notice that the forceRead() method is called inside a dedicated Thread from the parent class InputStream, so it is safe to use that while loop inside.

Result

Let’s watch and enjoy our app (using the engine as in Demo.java) :