When it comes to building responsive Android apps, there are two commandments:

Don’t block the UI thread, ever. Only access and modify the UI from the UI thread itself.

The Android Dev Guide goes into some detail about this. The usual solutions in Java are to:

Spawn a thread that does work and then invoke View.post with a Runnable that manipulates the UI.

with a that manipulates the UI. Implement and run an AsyncTask that does work in doInBackground , and then manipulates the UI in onPostExecute .

As you can see, both these solutions — though they are the bread and butter of Android development — are pretty verbose and unwieldy:

public void onClick(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png"); mImageView.post(new Runnable() { public void run() { mImageView.setImageBitmap(bitmap); } }); } }).start(); }

public void onClick(View v) { new DownloadImageTask().execute("http://example.com/image.png"); } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } }

I prefer AsyncTask , which has better semantics, but implementing one AsyncTask after another quickly becomes tedious. Same with writing line after line of Runnable boilerplate.

Enter Scala, using either the sbt Android plugin or my Maven-based solution.

The first step is to create an implicit conversion from a function to a Runnable , which can then be passed to View.post or runOnUiThread . Once we’ve done that, it’s simple to use spawn from scala.concurrent.ops to do background work.

Here’s a fully functional example:

package com.rrsllc.android import _root_.android.app.Activity import _root_.android.os.Bundle import org.apache.http.impl.client.DefaultHttpClient import org.apache.http.client.methods.HttpGet import scala.concurrent.ops._ class MainActivity extends Activity with TypedActivity { def textview = findView(TR.textview) implicit def toRunnable[F](f: => F): Runnable = new Runnable() { def run() = f } override def onCreate(bundle: Bundle) { super.onCreate(bundle) setContentView(R.layout.main) textview.setText("hello, world!") spawn { val client = new DefaultHttpClient() val httpGet = new HttpGet("http://www.android.com") val result = try { val response = client.execute(httpGet) "The response status was: " + response.getStatusLine.toString } catch { case _ => "Something went wrong." } runOnUiThread { textview.setText(result) } } } }

This is so much nicer than the Java alternative. Instead of up to ten lines of boilerplate for each background task, we end up with just the spawn and runOnUiThread .

If you’re waiting for that final push to switch over to Scala for your Android development, I highly recommend the sbt plugin for Android. Take a look at its brilliant Getting Started guide, which will have you up and running in just a few minutes.