Home > Android, Xtend > Xtending Android development

Xtending Android development

Android is an amazing operating system that combines technical innovation with high popularity among the end-consumers. In spite of fragmentation, there is a large amount of developers across the world who enjoy developing applications for Android. Today, most Android applications are written in Java. Unfortunately, Java is known to be deficient in some modern language concepts and idioms like conciseness, declarativity, modularity mechanisms, the map/reduce pattern or immutable data. Wouldn’t it be great if there was a more advanced programming language for Android development? Luckily, there is one and it integrates seamlessly with Java. This language is Xtend.

Xtend is a statically-typed programming language that compiles to Java. Xtend has a clear and concise syntax. Although lambda expressions will be finally introduced in Java 8, Xtend’s syntax for them is significantly more readable. Another feature that makes Xtend a functional language is the absence of statements. Code written in Xtend is executed by evaluating expressions. Operator overloading in form of an operator-to-method mapping makes possible to fully customize operator semantics. Extensions methods allow for enhancing closed types with new functionality. Although the language is statically typed, its polymorphic method invocation combined with the use of extension methods brings duck typing to Xtend. Other powerful features like the type-based switch expression, shorthand property access and local type inference make programming in Xtend a real fun. But what probably even more important than the language features is the full, bidirectional interoperability with Java, a low learning curve for Java developers, comprehensible and efficient generated Java source code and a great IDE support. All this makes Xtend ready for immediate use in every environment where Java and Eclipse are used for development. If you are new to Xtend, check out its documentation.

In this tutorial, we will write an Android application in Xtend, build an APK and run it on the Android emulator. We will implement a simple application called Is Following? which let us check if a Twitter user follows another Twitter user.

Is Following?

To be able to follow the tutorial, you need to be acquainted with Android development and the Eclipse IDE.

Setting up the IDE

First, we’ll install and configure the IDE and create a project. You can skip the first three steps, if you already have an Eclipse installation that you use for Android development.

Install Xtend 2.4.0

  • Click Finish and follow the instructions of the installation wizard.

Creating a project

Now, we’ll create a project and start writing the application. You can download the complete source code from https://github.com/nossipova/isfollowing.

  • Create a new Android Application Project with only one blank activity MainActivity.

New Android project

  • The compiler compliance level of the project needs to be set to 1.6.

Creating a layout

Create a simple layout that consists of:

  • an editable field for each of both Twitter names,
  • a button to start the request and
  • a text view that will show a result.


Writing the application

Our application will have an activity to interact with the user and a communicator which will send requests to Twitter and process its responses. The communication with Twitter will be performed asynchronously. Let’s implement these components.

  • Delete the class MainActivity.java that was automatically generated through the project creation.
  • To create a new Xtend class, select New -> Xtend Class from the project context menu in the Package Explorer.
  • On the first page of the creation wizard, type MainActivity in the Name field and select android.app.Activity as a superclass.
  • Click Finish.
  • The class we’ve just created will show an error:
    Mandatory library bundle 'org.eclipse.xtext.xbase.lib' 2.3.0 or higher not found on the classpath.

    Use the available quick fix to add Xtend libraries to the classpath.

  • MailActivity will have two methods: onCreate(Bundle) and showResult(Object). The first method onCreate(Bundle) prepares the content view and registers the click listener callback of the Check button. In the callback, we initialize the communicator, use its method isNetworkAvailable(Context) to test if network is available and execute the communicator asynchronously. The second method showResult(Object) updates the UI with the response received from the communicator by setting the text value in the result text view.
package de.nataliaossipova.isfollowing

import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class MainActivity extends Activity implements AsyncDelegate {

    override onCreate(Bundle it) {

        val button = R$id::Button01.findViewById as Button
        button.onClickListener = [
            var communicator = new TwitterCommunicator
            communicator.delegate = this as AsyncDelegate
            if (communicator.isNetworkAvailable(this)) {
                val editText1 = this.findViewById(R$id::EditText01) as EditText
                val editText2 = this.findViewById(R$id::EditText02) as EditText
                communicator.execute(editText1.text.toString, editText2.text.toString)

    override showResult(Object it) {
        var isFollowing = Boolean::parseBoolean(it as String)
        var resultView = this.findViewById(R$id::ResultView) as TextView
        resultView.text = if (isFollowing) {
        } else {
  • Create a new Xtend class TwitterCommunicator as a subclass of android.os.AsyncTask. The subclass overrides two methods: doInBackground(Params...) and onPostExecute(Result). In doInBackground(Params…) method, we create a new Twitter URL from both Twitter names passed in as parameters. After that, we open an HTTP connection and read the input stream from it. The second method passes the data read from the input stream to the main activity.
package de.nataliaossipova.isfollowing

import android.content.Context
import android.net.ConnectivityManager
import android.os.AsyncTask
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

class TwitterCommunicator extends AsyncTask<String, Void, String> {
    private AsyncDelegate delegate

    override protected doInBackground(String... it) {
        var url = new URL('https://api.twitter.com/1/friendships/exists.json?screen_name_a=' + get(0) + '&screen_name_b=' + get(1))
        (url.openConnection as HttpURLConnection).inputStream.readStream

    override onPostExecute(String it) {
        if (delegate as Object != null) {

    def setDelegate(AsyncDelegate it) {
        delegate = it

    def isNetworkAvailable(Context it) {
        val networkInfo = (getSystemService(Context::CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
        if (networkInfo != null && networkInfo.connected) { true } else { false }

    def private readStream(InputStream it) {
        var reader = new BufferedReader(new InputStreamReader(it))
        var line = ''
        var sb = new StringBuilder
        while ((line = reader.readLine) != null) {
        if (reader != null) {
  • To avoid tight coupling between the activity and the communicator, we will make use of the delegate pattern. Create a new Xtend class AsyncDelegate. This time it will be an interface, so replace the keyword class with a keyword interface in the newly created Xtend class. The interface declares only one method showResult(Object). We will call this method from the post execute callback of TwitterCommunicator.
package de.nataliaossipova.isfollowing

interface AsyncDelegate {
    def void showResult(Object it)

Our MainActivity already implements the AsyncDelegate interface.

Build and Run

Now, when we are done with writing source code, add permissions android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE to the Android manifest and let’s build an APK and run it on an AVD.

We need to tell ADT how to export the Xtend libraries. Choose Properties -> Java Build Path from the project context menu and go to the Order and Export tab. Select the checkbox beside Xtend library.


After closing the Properties window, you can launch the application on an existing AVD or a device.

Where to go from here

Now that you know how to use Xtend for Android development, go and create awesome Android apps in this sophisticated programming language!

Don’t hesitate to let me know if anything of the above is confusing to you or if you’d like me to go into more detail.

Tags: ,
  1. 3. August 2014 at 17:26

    Very interesting. Thanks. However, the twitter returns the following message:
    {“errors”:[{“message”:”The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.”,”code”:64}]}

  2. 8. August 2014 at 11:00

    Twitter retired API v1.0 in June 2013. I might update this blog post some day but for now, you need to figure out by yourself how to use the new API version.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s