Tools

Developing Android Apps with Scala and Scaloid: Part 2

Starting with templates, Android features can be added quickly with a single line of DSL code.

Executing Methods from System Services

Scaloid defines a SystemService trait that simplifies the process of creating system service objects and makes the code that executes methods from system services easier to read. For example, if you want to play the sound effect of key clicks, you have to use the android.media.AudioManager. In order to get an instance of this class in Scala, you must use:

Thus, with Scaloid, you just need to call the necessary method for audioManager. You don't need to worry about the Context and you can start working with the retrieved AudioManager instance. The following line plays the sound effect of the key clicks:

audioManager.playSoundEffect(android.media.AudioManager.FX_KEY_CLICK)

If you want to know whether any music tracks are active, you can evaluate the following expression:

audioManager.isMusicActive()

Another simple example is to make the device vibrate for 1 second. Just call the vibrate method for vibrator, as in the following line.

vibrator.vibrate(1000)

You work with the retrieved android.os.Vibrator instance with vibrator.

As you might guess, the only rule you must consider is that Scaloid uses lowercase for the first character (as opposed to uppercase with the Android SDK class name). So, if you want to call methods for android.app.WallpaperManager, you instead need to use wallpaperManager.

Simplifying Alert Dialogs

When you want to display an alert dialog, the android.app.AlertDialog.Builder class provides many methods that you can use to build the dialog. However, you usually must call many methods to set the title, the message, the buttons, etc. In addition, you have to create a listener for each button you define. For example, if you want to build an alert dialog to confirm a request for authentication when the user clicks on the Sign In button, the code would need to perform the following operations:

Create an instance of android.app.AlertDialog.Builder

Call the setTitle method with the desired title: "Authentication confirmation"

Call the setMessage method with the desired message: "Are you sure you entered the right password?"

Call the setPossitiveButton method with the desired title "Yes" and a new DialogInterface.OnClickListener instance. Then, define the onClick method for this instance with the code that must be executed when the user clicks the button

Call the setNegativeButton method with the desired title "No." In this case, there is no need for a DialogInterface.OnClickListener instance because this button won't have any effect

Call the show method from the UI thread to display the dialog

Scaloid provides an AlertDialogBuilder class that reduces the necessary code to build a complex alert dialog. You can easily define the buttons that the dialog must display by using positiveButton and negativeButton. You can define the actions that must be performed when the user clicks a button without having to define a listener, with the same syntax you use when you associate actions to widgets by using the enriched prefixed classes I explained in the previous article.

The following lines build and display our sample alert dialog, which required many method calls, with a less-complex syntax by taking advantage of the AlertDialogBuilder class (see Figure 6). When you call the show method, there is no need to worry about the UI thread because the class handles the execution on the UI thread for you.

Figure 6: The App displaying the alert dialog built with the AlertDialogBuilder class.

Conclusion

You can use Scala or a Scala/Java mix to develop your Android apps. However, when you do this, it is a bit more complicated to take full advantage of Scala's features because the Android SDK targets Java. Scaloid provides a convenient bridge for Scala developers to tackle the Android SDK with a more appropriate DSL. If you have good experience developing Android apps, you probably won't like the idea of defining the UI with Scala code instead of the classic XML layout definition. However, I'm sure Scaloid will provide great value if you want Scala to be your main programming language for your Android apps. The final code for an Android app with Scaloid is really easier to read and maintain, and therefore, is a good way to take advantage of Scala features to simplify the problems of a specific domain  in this case, Android app development.

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.

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!