Android view construction

An Android user interface is constructed from View objects. The following is an example that illustrates some features of Kawa to help write views hierarchies, The example is self-contained, and can be built and run as described in Building for Android.

(require 'android-defs)
(activity hello
  (on-create-view 
   (define counter ::integer 0)
   (define counter-view
     (TextView text: "Not clicked yet."))
   (LinearLayout orientation: LinearLayout:VERTICAL
    (TextView text: "Hello, Android from Kawa Scheme!")
    (Button
     text: "Click here!"
     on-click-listener: (lambda (e)
                          (set! counter (+ counter 1))
                          (counter-view:setText
                           (format "Clicked ~d times." counter))))
    counter-view)))

The first import form imports various useful definitions from the Kawa Android library. Using these is not required for writing a Kawa application, but makes it more convenient.

The names LinearLayout, TextView, and Button are just aliases for standard Android View sub-classes. A few are prefined by (require 'android-defs), or you can define them yourself using define-alias.

An Android application consists of one or more activities, each of which is an instance of the android.app.Activity class. You can use the activity macro to define your Activity class. The first macro argument (in this case hello) is the class name, and the others are members of the class, in the syntax of a field-or-method-decl. The sub-form on-create-view is an abbreviation for declaring an onCreate method (which is called when the Activity starts up followed by a setContentView: The body of the on-create-view is evaluated. The result should be a View expression, which is passed to setContentView.

Procedure: current-activity [new-value]

With no arguments, returns the current Activity. If a new-value argument is given, sets the current activity. It is set automatically by the on-create and on-create-view methods of the activity macro.

Since current-activity is a parameter object, you can locally change the value using parameterize.

View object allocation

To create an instance of a View class you “call” the class as if it were a function, as described in Allocating objects. For example:

(TextView (this) text: "Hello, Android from Kawa Scheme!")

If you (require 'android-defs) that defines some special handling for View classes. You can leave out the (this) argument, which refers to the enclosing Activity:

(TextView text: "Hello, Android from Kawa Scheme!")

Event handlers

You can register event listeners on Android View objects using methods typically named setOnEVENTListener. For example setOnClickListener. When allocating an object you can leave out the set, and you can optionally use Scheme-style names: on-click-listener. The argument is an object of a special nested listener class, for example View$OnClickListener. These are single-method classes, so you can use a lambda expression and SAM-conversion will automatically create the needed listener class.