Update 2

The build file is upgraded to have “Wizard-like” choices. No need to choose which target to run, just right-click on the “wizard.xml” and run as ant build.

Update

Just released an ant build file to help generating activity and application. The following tutorial is updated with using ant build.

For those who are already using Android-Binding

I just released the Android-Binding 0.5, this is the first official release to support features in HC/ICS, which includes:

  1. Action Bar
  2. Action Modes

As mentioned in previous post, we decided not to include fragment and we provide alternatives to fragments, and those already included in previous releases.

If you are targeting platforms prior to HC, do you need this update? Yes, because we also included binding to ViewPager and to Activity itself.

Please notice about the new binding to activity xml metadata file, as we are deprecating the setAndBindRootView() method.

For everyone

Android-Binding is a MVVM framework for used in Android. It help separate the View (widgets) from View Model (Java codes). This post should be your first post into working with Android-Binding.

Hello World

Before starting the tutorial, please make sure you have:

  1. A working Eclipse that satisfy the requirement of Android development
  2. Download the latest version of Android-Binding (v0.5+)
  3. Download the Ant Build Package (optional, but highly recommended)

In Eclipse click to File > new > Project, follow the following steps:

Build Target set to 4.0 (+), we are going to support multiple configurations.

Min SDK to anything larger than 4 (1.6), Android-Binding supports SDK from 4, and here we just use 8.

Now, we have a basic project to start with. Next we are going to install the android-binding library to the project.

Enabling Android-Binding

Open the workspace folder of Eclipse (in Windows, most probably it is under c:\Users\USERNAME\workspace), under the project folder (HelloAndroidBinding), create a sub-directory called “libs”.

Put the downloaded jar (android-binding-v30.version.jar) into that libs folder.

Extract the antbuild.zip to the root project folder, there should be a “android-binding.ant.jar” file and a “build.xml” file in it.

Back to Eclipse, on Package Explorer, hit F5 to refresh the folder. You should have something like this afterwards:

As seen above, under libs, we have the jar we included, and ADT helps us to setup the link to the library and in Android Dependencies section, we can see the android-binding library there.

We need an application file to initialize Android-Binding, which we can easily created by using the build.xml file. Right Click on the build.xml, and choose Run As -> 2 Ant Build…

And check the Create Application Task and Run:

When prompted with Application Class Name, put in “HelloAndroidBindingApplication” (or anything you want). If you look at the console, it will print a “BUILD SUCCESSFUL” if no problem arise.

The Application file is generated, hit “F5″ in project root folder and you will find this:

Which, the file is generated by Ant.

If you look at the file, you should have something similar to below:

We need to init our Binding Engine, putting BinderV30.init() is all we need. (Note, calling BinderV30 will enables ICS+ features).

Create the Activity Class

We now use Ant build to setup our sample activity class and relevant resource files.

Right Click on Build.xml, choose run as-> 2. Ant Build…

This time, we check the “new-activity” task and uncheck the rest, click run.

When prompted with Activity Name, we put “HelloAndroidBindingActivity” in it. After a short while, “BUILD SUCCESSFUL” appears and refresh the project folder, we have following stuff:

  1. xml/{name}_metadata.xml: the metadata file for binding the activity
  2. xml-v14/{name}_metadata.xml: metadata file with Action Bar, only readable by SDK14+ machines
  3. layout/{name}_layout.xml: main layout file (root view)
  4. menu/{name}_optionsmenu.xml: Options menu template
  5. src/{name}.java: The generated activity class

Result

Now run the program, welcome to MVVM.

About these ads