Mobile

Developing Android Apps with Scala and Scaloid: Part 1

Build a UI layout by writing type-safe Scala code and wire your logic into the layout

Working with Styles

The Android SDK allows you to specify styles and themes in XML resources separate from the XML that defines the UI layout. Scaloid takes a different approach  providing the necessary helpers to allow you to define styles with Scala code.

You can change the values of the properties for both the widget and its related LayoutParams object by calling different methods. Scaloid mimics the XML attributes and also provides new methods that reduce the code required to specify values for common properties. For example, the following line sets the text color, the background color, and the text size for the EditText widget. In addition, the code calls the margin method for the related LayoutParams object with a single 25 dip parameter to specify the same value for the margin in the four directions: top, right, bottom, and left.

This one line made changes to an individual widget. Scaloid allows you to use a SViewGroup.style(View => View) method to apply styles by taking advantage of Scala's pattern matching and type safety. The style specification must return the view component that has to be shown on the screen; therefore, you can even return a completely new widget. The following lines show a new version of a simple Scaloid UI that uses the style method to set the values of different properties according to the matching widget type and return the actual component that has to be shown on the screen. This way, the style information is in a different block, separated from the UI definition.

Figure 3: An Android device emulator displaying the UI generated with the Scaloid code and the styles applied to each widget.

You can easily override any property value by calling the necessary methods for each widget. For example, the following line would make the text size of the TextView larger than the default value specified by the style pattern matching:

STextView("Enter your password").textSize(45 sp)

As seen in the code, you can easily work with the dip and sp units by adding them after the value. Scaloid defines an internal UnitConversion class and a ConversionImplicits trait that makes it easy to convert from either dip or sp to pixel units. For example, the following line converts 45 sp to pixel units:

val textSizeInpixels:Int = (45 sp)

Scaloid provides other useful implicit conversions such as String to Uri and Int resource IDs to the following types:

Array[CharSequence]

Array[String]

CharSequence

Drawable

Movie

Working with Listeners

The listener interfaces in the Android API allow you to code actions in callbacks that will be executed when you're notified that a specific event occurs. Scaloid uses many shortcuts to simplify the code required to append code to listeners. For example, the following line defines the code that has to run when the user taps the "Sign in" button. The openUri method opens the URI in a Web browser or in a view assigned to the HTTP protocol. Remember that Scaloid provides an implicit conversion from String to Uri.

SButton("Sign in").onClick(openUri("http://drdobbs.com"))

The onClick method is a listener-appending method that provides a shortcut to the more complex setOnClickListener. As you might guess, Scaloid listener-appending methods remove both the set prefix and the Listener suffix; therefore, you can use onTouch instead of setOnTouchListener and onKey instead of setOnKeyListener. The previous line is equivalent to the following longer code:

If you have Android development experience, you already know a toast provides simple feedback about an operation in a small popup. The following code uses the default listener callbacks and a call to Toast.makeText to display "You pressed the Sign in button" in a small popup with a short length when you click or tap the "Sign in" button (see Figure 4). Again, forgive me for using hardcoded strings that should be string resources.

Scaloid provides a toast method that displays the popup that will be displayed for a brief period of time by default. The following line uses both the click-listener-appending and the toast methods to reduce the code to a single line:

SButton("Sign in").onClick(toast("You pressed the Sign in button"))

You can call the toast method without worrying about the thread in which you are running because the method makes sure that the call to Toast.makeText runs on the UI thread. This way, the code is easier to read because you don't have to make calls to the Activity.runOnUIThread method.

Conclusion

Scaloid is a good example of how you can use Scala's features to simplify code and implement a new way of creating Android apps with a DSL. Scaloid provides many interesting features to simplify asynchronous task processing and the execution of methods from system services. I'll explore them in the next article. I'll also discuss important classes and traits that you must understand when working with Scaloid. This way, you will have a good idea of many important features offered by Scaloid and can decide whether it is a good fit for your Android projects.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!