Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Developing Android Apps with Scala and Scaloid: Part 2

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:


The SystemService trait provides audioManager with the following code:

@inline def audioManager(implicit context: Context) = context.getSystemService(Context.AUDIO_SERVICE).asInstanceOf[android.media.AudioManager]

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:


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


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


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:

  1. Create an instance of android.app.AlertDialog.Builder
  2. Call the setTitle method with the desired title: "Authentication confirmation"
  3. Call the setMessage method with the desired message: "Are you sure you entered the right password?"
  4. 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
  5. 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
  6. 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.

SButton("Sign in").onClick({
	val question = new AlertDialogBuilder("Authentication confirmation", "Are you sure you entered the right password?") {
	    val progressDialog = spinnerDialog("Scaloid App", "Authentication in progress...")
		spawn {
			      val result = runJob("http://drdobbs.com")
			      alert("Welcome to the Scaloid App. Check the results:", result)

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


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.

Gaston Hillar is a frequent contributor to Dr. Dobb's.

Related Reading

Developing Android Apps with Scala and Scaloid: Part 1

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.