Reinaldo Junior

Um outro tech-blog

Playing with Droid

During the last weeks I’ve been working on the Droid DSL. Now I’m proud to announce that it is working!

Since the last blog post, the DSL had changed. In order to simplify the DSL some concepts have been removed, for example, ImageButton, Link and ImageLink. These concepts are still available at the Button concept, so a Button with and “src” property is a ImageButton and a Button with a “target” property is a “Link”.

Another significant change in the DSL is the merging of the Activity and Layout concepts in a single concept: Screen (although the layout concept is still existing). This made easier to write a simple application. The screen’s widgets can now be placed inside the screen (you don’t need anymore to create a layout to put the widgets and set the activity to show the created layout).

So what used to be the simplest Droid example:
application "Hello, Android!" com.example.android.hellodroid {

string hello = "Hello, Android! I am a string resource!"

activity HelloDroid {
=> show main
}

layout main {
textView: @string hello
}
}

Now is even simpler:
application "Hello, Android!" => com.example.android.hellodroid {

string hello = "Hello, Android! I am a string resource!"

screen HelloDroid {
# textView: @string hello
}
}

In order to play with the Droid DSL, you need to install some plugins:
  1. Install the ADT Eclipse plugin. The target of the DSL is creating Android applications, so you must to install the ADT. The Android Hello World tutorial is all you need to have your environment ready to the work.
  2. Install the Droid plugins, located at this Update Site: http://reinaldojunior.net/eclipse-amalgamation-tutorials/

Creating your first Droid Project

A Droid project is an Eclipse Android Project that has automatic code generation from a droid model. The droid model is inferred from a .droid file (a textual representation of the model).

To create a Droid project you must to:
  1. Create an Android Project (following the instructions at the Hello World tutorial)
  2. Create a ’model‘ folder inside the project
  3. Create a ’src-gen’ folder inside the project
  4. Right-click the ’src-gen’ folder and select “Build Path” > “Use as source folder”
  5. You can delete the content of the ’src’ folder as it will have a generated Activity stub.
Now you are ready to start playing with the Droid DSL. Inside the ’model’ folder, create a .droid file and open it. The file will be opened with the DSL textual editor (generated with Xtext).

You can use this sample code to test your environment:
application "SimpleWizard" => org.eclipse.amalgam.tutorials.android.simplewizard {

screen First {
# textView: "This is the first screen" {}
# button: "Next >" to Second {}
}

screen Second {
# textView: "And this is the second screen" {}
# button: "< Back" to First {}
# button: "Next >" to Third {}
}

screen Third {
# textView: "Now, the last one" {}
# button: "< Back" to Second {}
}
}

Generating the code

  1. Right-click in the .droid file
  2. Select “Acceleo Model to Text” > “Generate Android Code”
  3. The generated code will be available at the ’src-gen’ folder and at the ’res’ folder.
In order to run the generated application, I recommend to use an Android 2.3.3 virtual device.