Ruboto Tip: Styling Interface Widgets
My earlier Android Meets Ruby: Using Ruboto UI blog post described how to use newer Ruboto syntax when constructing your user interfaces. Ruboto also has support for styling your interface widgets, but you may run into some issues at runtime depending on your environment setup. Fortunately, a workaround is available.
Suppose we want to modify the Fishmonger app to include larger text, so customers can more easily read the prompts and order their fish. In standard Android, we would accomplish this with the following XML:
<TextView android:text=”What would you like to order?” style="@android:style/TextAppearance.Large"/>
In Java, you can accomplish the same task by providing the corresponding style attribute value to the constructor:
new TextView(this, null, android.R.attr.textAppearanceLarge);
Ruboto is adding similar support to its widget scripting. Instead of using kludgy Java syntax to replicate the above constructor, you’ll be able to use a more natural syntax:
text_view :text => 'What would you like to order?', :default_style => R::attr::textAppearanceLarge
However, when you run this script, you may encounter an error like the following when the app tries to create your UI:
04-17 21:40:17.789: I/System.out(9291): NoMethodError: private method `attr' called for Java::Android::R:Class 04-17 21:40:17.789: I/System.out(9291): on_create at file:/data/app/us.cirion.fishmonger2-2.apk!/fishmonger2_activity.rb:29 04-17 21:40:17.789: I/System.out(9291): linear_layout at (eval):20 04-17 21:40:17.789: I/System.out(9291): on_create at file:/data/app/us.cirion.fishmonger2-2.apk!/fishmonger2_activity.rb:21 04-17 21:40:17.793: I/System.out(9291): start_ruboto_activity at file:/data/app/us.cirion.fishmonger2-2.apk!/ruboto/activity.rb:49 04-17 21:40:17.793: I/System.out(9291): (root) at file:/data/app/us.cirion.fishmonger2-2.apk!/fishmonger2_activity.rb:18
What’s going on here? There are several things to check for. First, make sure that you’re referencing the proper android.R, and not your project’s own .R class. The latter is usually referenced via Ruboto::R. You may also run into issues where ruboto or jruby try to grab the wrong field in R.
Fortunately, there’s a workaround available that uses a more verbose way of accessing this field:
@text_view = text_view :text => 'What would you like to order?', :default_style => JavaUtilities.get_proxy_class("android.R$attr")::textAppearanceLarge
This code will directly look up the attributes class through the proper package, and then retrieve your desired style value. While this admittedly looks just as kludgy as the constructor we’re trying to get away from, it still better fits the Ruboto way of constructing widgets by configuring the parameters. Hopefully you don’t ever need to solve this problem, but if you do, keep this escape hatch in mind.
Safari Books Online has the content you need
Check out these Android books available from Safari Books Online:
|Android in Action, Third Edition is a fast-paced book that puts you in the driver’s seat–you’ll master the SDK, build WebKit apps using HTML 5, explore cross-platform graphics with RenderScript, learn to use Native Development Kit, and master important tablet concepts like drag-and-drop, fragments, and the Action Bar, all new in Android 3.|
|Beginning Android 4 is fresh with details on the latest iteration of the Android platform. Begin at the beginning by installing the tools and compiling a skeleton app. Move through creating layouts, employing widgets, taking user input, and giving back results.|
|Android in Practice is a treasure trove of Android goodness, with over 90 tested, ready-to-use techniques including complete end-to-end example applications and practical tips for real world mobile application developers. The book dives into important topics like multitasking and services, testing and instrumentation, building and deploying applications, and using alternative languages.|
|Android UI Fundamentals: Develop and Design walks developers through the different choices available on their way to creating a well-designed application for Android. While building a simple application, Jason works through the basics of Android UI development including layout, event handling, menus and notifications.|
|Programming Android shows experienced application developers what they need to program for the Android operating system — the core building blocks, how to put those blocks together, and how to build compelling apps that work on a full range of Android devices.|
About this author
|Chris King is a software engineer specializing in mobile development. He is the author of Advanced BlackBerry Development, Advanced BlackBerry 6 Development, and Android in Action, Third Edition. You can learn more about Chris at www.linkedin.com/pub/chris-king|