[Lucas Galego] Ruboto, this android works?

I was wondering about play a little with a project called Ruboto by some time.

For who doesn’t heard anything about this project, Ruboto is a intended to make possible to write android applications with Ruby.

Since I love my old but gold Motorola Milestone and I also love Ruby this sounds really fun.

Here is what I have done:

My intention was to make a really simple application just to see the maturity of the project.

What can be more simple than a planning poker app? Well I don’t know. So I decided to make that one.

Begining from scratch:

  • You can find more about the Ruboto project at its web page.
  • You can write fully funcional apps only writing code in Ruby and using generators to make the Java wrappers.
  • You can make hybrid projects using Java and Ruby.
  • It cover almost all android API.

But… There is always a but:

  • To run Ruboto apps  you have to install the ruboto-core app to your android device.
  • If you want that your app support JIT (and who doesn’t want that?) your apk file will get bigger.

Let’s Play

To start to play you have to setup your enviroment.

You gonna need the JDK installed in your system.

Once you it installed, install the JRuby.

I recommend you to use the JRuby 1.6.4. It will also be smart, install it using rvm and create a gemset to full around.

Another dependency is the Android SDK. You can get it here.

Read the REAME file since you have downloaded it. ;)

Don’t forget to install the android sdk and the development tools.

Okay. That’s all. Now we can start the party.  :)

First we need to create an emulated device to use during our development.

To create this run the command:

android -s create avd -f -n Android_2.3 -t android-10 –sdcard 64M

To startup the emulator you created:

emulator -avd Android_2.3

Generating the Ruboto project

ruboto gen app –package better.code.planning.poker –target android-10

Just this. The Ruboto project contains code generators like Ruby on Rails.

You have now:

  • All the project structure of an Android project,
  • All the Java wrappers to your Ruby script,
  • A Hello World Ruby Script,
  • Some usefull rake tasks.

The rake tasks are:

  • rake -> Build the apk.
  • rake install start -> Build the apk, install and start it on your emulator.
  • rake install:clean start -> Also clean the previous build from your emulator.
  • rake update_scripts:restart -> Updates only the ruby script on your device, it can save you some compilation time.

You can also make the deploy to the device by yourself using the comand:

adb install ruboto-core.apk

Let’s start to code

First lets define a better title to our activity:

Edit the application name into the file: res/values/strings.xml

<string name="app_name">Planning Poker!</string>

Now we can finally see one of the good points of using Ruboto.

When developing Android apps, you have to deal with a lot of xml files that handle how the templates will be defined.

With Ruboto, we can define our template using its much more delicious DSL.

Edit the file: src/poker_activity.rb

require 'ruboto'

java_import 'android.content.Intent'

ruboto_import_widgets :Button, :LinearLayout, :TableLayout, :TableRow

$activity.handle_create do |bundle|
  setTitle 'Lets make this Sprint!'

  setup_content do
    table_layout do
        table_row do
          button :text => ' 1 ', :text_size => 57
          button :text => ' 2 ', :text_size => 57
          button :text => ' 3 ', :text_size => 57
        end
        table_row do
          button :text => ' 5 ', :text_size => 57
          button :text => ' 8 ', :text_size => 57
          button :text => ' 13', :text_size => 57
        end
        table_row do
          button :text => ' 20', :text_size => 57
          button :text => ' 40', :text_size => 57
          button :text => '100', :text_size => 57
        end
        table_row do
          button :text => 'Zzz', :text_size => 57
          button :text => ' 0 ', :text_size => 57
          button :text => ' ? ', :text_size => 57
        end
    end
  end
end

We defined our layout using the Ruboto DSL with the methods setup_content, table_layout and table_row.

We set the content of each row with the method button.

Events and new Activities

Now let’s make the magic. When the user press the number he wants to vote, we will popup a new Activity with the value.

For this we need to create a new Activity.

ruboto gen class Activity –name Card

And edit the code in src/card.rb like this:

require 'ruboto'

ruboto_import_widgets :Button, :LinearLayout, :TextView

$activity.handle_create do |bundle|
  setTitle 'My vote is'

  setup_content do
    linear_layout :orientation => LinearLayout::VERTICAL do
      @text_view = text_view :text => $activity.getIntent.getExtras
                   .get("better.code.ruboto.planning.poker.vote"),
                   :text_size => 150
    end
  end

end

Now, what we need is register the event, so we can dispatch the new Activity.

First we need to register the new activity in the AndroidManifest.xml file

<activity android:name='Card' android:label='Card'>
        <intent-filter>
            <action android:name='android.intent.action.CARD'/>
            <category android:name='android.intent.category.DEFAULT'/>
        </intent-filter>
</activity>

Now, we add the handler of the action into the src/poker_activity.rb file. Just after the setup_content method:

handle_click do |view|
    i = Intent.new("android.intent.action.CARD")
    i.putExtra("better.code.ruboto.planning.poker.vote", view.text)
    startActivity(i)
end

Using the Intent instance, we can call the putExtra method. That’s the way we can pass args to our new Activity.

In the Card Activity, the args are rescued using the getExtras method.

That’s it. I hope you enjoy it just like me :)

See ya!