-
Notifications
You must be signed in to change notification settings - Fork 1
Overview of droiuby ruby scripts
This section introduces scripting in droiuby using the ruby language. For reference on the ruby language visit the main ruby site here
Droiuby uses an implementation of ruby called JRuby which is specialized for deployment on a java based VM. JRuby allows for direct access to defined Java classes which makes it simpler to integrate ruby with the android framework. This is in contrast to javascript based interpreters which need to rely on a bridge mechanism with WebView to allow native access to the android framework.
Note that Views and android widgets are used interchangeably in this document. Views are the basic UI building blocks in android (seeing as all of the UI elements extend from it). Buttons, Layouts, fields and almost anything rendered on screen is a view.
Droiuby ruby scripts are primarily loaded via the controller attribute specified in the main layout file. Ruby scripts are executed after the droiuby activity builder is done with setting up the view heirarchy. Droiuby scripts don't have to be suffixed with a .rb script, if you are using a web framework like rails, you can even generate those scripts on the fly. A droiuby script requires at least an on_create method defined. The on_create method is called immediately after the views are ready. Attachment of event handlers are usually done at this point as well as further refinements on the view hierarchy like hiding of sections or populating sections with new views.
Below is a sample script which some of the common tasks:
class Main < Activity
def on_create
puts 'Hello world from controller file v1'
V('#test_field').tap do |text_field|
text_field.text = 'prefs here'
text_field.on(:focus_changed) { |v, has_focus|
toast "text field has focus " if has_focus
}
end
if _P.contains? :some_text
some_text = _P.get(:some_text)
puts "Setting text #{some_text} from preferences"
V('#test_field').text = some_text
end
V('#store_text').on(:click) do |v|
toast 'storing in prefs'
_P.update_attributes!(some_text: V('#test_field').text)
end
V('#test_button').on(:click) do |v|
puts "test_button #{v.id} was clicked!!!!!! via on clicked"
toast 'test_button was clicked!!!'
V('#section').inner = '<t size="20">Clicked!!!!</t>'
#animation
V('#section').animate { |t|
t.alpha 0, 1, {duration: 2000}
}.with(
V('#test_button').animate { |t|
t.alpha 1, 0, {duration: 1000}
}
).start
#async task demonstration
async.perform {
http_get "asset:hello_world/_hello_world.xml"
}.done { |result|
V('#hello_world_section').inner = result
}.start
end
V('#test_button').on(:long_click) { |v|
puts "This button was long clicked!!!!!!"
activity_instance_method('hi')
V('#section').inner = '<t size="20">Long Clicked!!!!</t><web src="http://www.google.com" width="match" height="match"/>'
async_get("asset:hello_world/_hello_world.xml") { |result|
V('#hello_world_section').inner = result
}
true #consume long click
}
end
def activity_instance_method(str)
puts "This instance method was called #{str}"
end
end
As you can see there are various helper methods that simplify a lot of tasks in android like selecting views (using the V function) and performing HTTP calls. For more details on how these, refer to the tutorials section here Tutorials.