Tutorial: Using ViewInflater
parroty edited this page Mar 31, 2013
·
10 revisions
The original tutorial shows how to dynamically create a GUI using objects.
However, there is an alternative, you can dynamically parse Android XML using the org.droiddraw.android.ViewInflater class from droiddraw.org
Create a new application
ruboto gen app --package org.ruboto.example.view_inflater --target android-14
Download ViewInflater.java
ViewInflater.java on Google Code
Install ViewInflater.java
mkdir -p src/org/droiddraw/android
cp ViewInflater.java src/org/droiddraw/android
Write the ruby code
require 'ruboto/widget'
java_import "org.droiddraw.android.ViewInflater"
ruboto_import_widgets :Button, :LinearLayout, :TextView
class ViewInflaterActivity
def on_create bundle
setTitle 'This is the Title'
inflater = ViewInflater.new(self)
text = "<?xml version='1.0' encoding='utf-8'?>" +
"<LinearLayout " +
"xmlns:android='http://schemas.android.com/apk/res/android' " +
"android:layout_width='fill_parent' " +
"android:layout_height='fill_parent'" +
"android:orientation='vertical' >" +
"<TextView android:text='Hello, I am a TextView' />" +
"<Button android:text='Hello, I am a Button' />" +
"</LinearLayout>"
view = inflater.inflate(text)
self.setContentView view
end
end
Compile and Run
rake
adb install ...
jsilver note: new edit's don't work for me.