[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.

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.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:text="@string/hello_world" />

        android:text="@string/button_accept" />

        android:text="@string/button_decline" />


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!

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 {

    public void onCreate(Bundle savedInstanceState) {

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

    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

2 thoughts on “[TUT] Using Annotations for Android XML onClick visibility

Leave a Reply

Your email address will not be published. Required fields are marked *