Tuesday 10 February 2015

Android App Development in Scheme


The Speech App using XML Layout

In our previous example we created a Text to Speech app in which we created an instance of the view object and passed it to the on-create-view macro. In this example we will use the android XML layout facility. You will see how simple our apps become.

First edit the "res/layout/main.xml" file to read as follows.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:name="main">
  <TextView android:text="@string/label_text"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>
  <EditText android:id="@+id/edit_text"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>
  <Button android:text="@string/button_text"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:onClick="onSpeakButtonClick"/>
</LinearLayout>

Notice the second to last line. We are setting the onClickListener of the button to a public method in the activity class called "onSpeakButtonClick". Next edit the "res/values/strings.xml" file to read as follows.


<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">hello</string>
  <string name="label_text">Enter the text to speak here</string>
  <string name="button_text">Speak!</string>
</resources>

Now let us look at the source code. Edit your "src/kawa/android/hello.scm" file to read as follows.


(require 'android-defs)

(activity hello
(mTts ::android.speech.tts.TextToSpeech)
(on-create
((this):setContentView kawa.android.R$layout:main)
(set! mTts
      (android.speech.tts.TextToSpeech
        (this)
        (lambda (i) ()))))
  ((onSpeechButtonClick (v ::android.view.View)) ::void
    (mTts:speak
      ((as <android.widget.EditText>
        ((this):findViewById
          kawa.android.R$id:edit_text)):getText)
      android.speech.tts.TextToSpeech:QUEUE_FLUSH
      #!null)))

First we create the android.app.Activity class using the 'activity' macro, with the name 'hello'.  Inside the hello class we define an instance variable and two methods. First we define the instance variable mTTs. Next we use the 'create-view' macro to create the classes 'onCreate' method. Here, we did not use the 'on-create-view' macro as we did in the previous examples. Thats because we want to call the 'setContentView' method ourselves.
 ((this):setContentView kawa.android.R$layout:main)
In the line above we 'setContentView' to 'kawa.speaker.R$layout:main'. We have to use the full qualified package name. We use the '$' in 'R$layout' because that is how inner classes are generated by the compiler.

If you noticed we did not call the super class onCreate method ourselves. This is called behind the scenes by the 'on-create' macro. This is one of the advantages in developing with scheme. We can simplify coding using macros. We then set! the mTts instance variable to a new instance of the android.speech.tts.TextToSpeech class.

We don't have a macro to create the onSpeakButtonClick method for us. So we define the method itself here. The rest of the code is the same as the one we saw in the previous example.
Now build your app and run it.

Reference: http://androidscheme.blogspot.in/2010/10/speech-app-using-xml-layout.html

No comments:

Post a Comment