Here is the source code for a simple app that will display a TextView and show a Button. When you click the button it will display a Toast message.
text: "Hello Android, from Kawa Android!")
text: "Click Me!"
"Beep Bop! You clicked me!"
You can see how simple and functional the whole code is. Even if you don't know scheme you can figure what is going on.
To get this code working on linux you will need to follow Per Bothner's blog post on how to set up your android development environment. Make sure you use the latest svn version of Kawa. For windows users you can follow the same equivalent procedure and it works. Get the hello example shown at the site working for you first. Copy and paste the code above into your hello.scm file and create the app.
Now for some explanation of the code. First we load the android-defs library, which loads some android definitions.
From Kawa Scheme we can interact with java virtual machine directly. Next we create an scheme expression to call the 'activity' macro. Scheme expressions have the form
(function & args)
Our expression is
(activity hello (....))
The activity macro creates an android.app.Activity with its first argument as name, which in this case is 'hello', and the second argument is an expression that will evaluate to an android.view.View object. The second argument in this case is the expression
which is also a macro that evaluates its argument expression and calls the setContentView method of the activity with the evaluated view. The rest of the code is for generating the view object.
In Kawa we can instantiate an instance of a class with this syntax
(MyClass keyword1: value1 ... kewordN: valueN)
where keyword corresponds to a field or method name. In the case of a field it will set the value of the field, and in case of a method it will call the method with the value.
In case you pass a lambda expression (anonymous function) as a value then Kawa will call the method with a corresponding anonymous class.
Also keywords need not match the method names exactly. Keyword 'text' will make the compiler look for the "setText" "addText" methods. "on-click-listener" gets translated to "onClickListener".
So this is what happens in the rest of the code. First we create an instance of the LinearLayout
to which we pass keyword value pairs. The first keyword sets the orientation. Then we call 'view' twice (Kawa will call the addView method of LinearLayout) passing it the TextView instance the first time and the Button instance the second time.
We create the TextView and Button object similarly. The on-click-listener of the Button object is passed a Lambda function which in turn instantiates a Toast object using its static method makeText. Kawa will automagically create the android.view.View.OnClickListener class as required by the onClickListener function of the button.
Now let us look at this part of the code.
"Beep Bop! You clicked me!"
First we call the makeText static method of the android.widget.Toast class which returns an instance of the Toast class and we immediately call its ':show' method. In kawa to access a field or method of an object use the ':' notation.