[TUT] Using Annotations for Android XML onClick visibility

In this tutorial I’ll show you how to create a basic Annotation. The idea being you can use this to highlight the entry points into your code. (These entry points coming from XML onClick declarations in your layout files).

I did this originally for myself, I was finding it hard when looking through my Activity classes to distinguish where the onclicks where declared. Pre-1.6? when I was setting on click listeners you could just go looking for the onClick method, but now I’ve got into the habit of using android:onClick=”” I can’t do this. Therefore adding a little annotation just gives my eye something to catch to easily spot these. It also gives the added benefit of not deleting the method by accident and only noticing this when you Force Close at runtime! Yes onclick methods are always declared public so I could just look for my public methods but methods can be public for other reasons as well so using an annotation is just nicer!

Here.. we … go:

Creating an Annotation is simple, just create a java file and instead of using the ‘class’ keyword use @interface. Done!: You might want to add a comment that will be shown later when you someone hovers over the annotation. This is just a little bit of helper text to explain what the annotation is.
FromXML.java:

package com.blundell.annotationtut.util;

/**
 * This method is declared in the Android XML layout file
 * @author paul.blundell
 */
public @interface FromXML {

}

I’ve added two buttons to the XML layout that declare their own on click methods. These are nice descriptive names of what the intention is of the button. I could have gone even more descriptive but coding style isn’t the aim of this tutorial.

activity_main.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAccept"
        android:text="@string/button_accept" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onDecline"
        android:text="@string/button_decline" />

</LinearLayout>

When you declare the click methods in your Activity you simple add the annotation. If you using eclipse it will even autocomplete it when you start typing. Add the import, add the annotation, done!
MainActivity.java:

package com.blundell.annotationtut.ui;

import com.blundell.annotationtut.R;
import com.blundell.annotationtut.util.FromXML;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @FromXML
    public void onAccept(View button){
    	Toast.makeText(this, "You clicked me! 1", Toast.LENGTH_SHORT).show();
    }

    @FromXML
    public void onDecline(View button){
    	Toast.makeText(this, "You clicked me! 2", Toast.LENGTH_SHORT).show();
    }
}

An image of the package structure

So I just thought this’d be a nice simple way of giving a bit of visibility to the entry points of you activity. You don’t have to use it, but if your reading this hopefully you will!

As usual any questions just ask.

Github repo:

Git Hub Annotation Repo

Eclipse source:
Annotation Eclipse Project Source