Skip to content
Pierre-Yves Ricau edited this page Feb 2, 2012 · 6 revisions

Since AndroidAnnotations 1.0

@Click

The @Click annotation indicates that an activity method must be called when the corresponding view is clicked.

The view id can be set in the annotation parameter, ie @Click(R.id.myButton).

If the view id is not set, the name of the method will be used to guess the view id.

The method may have zero or one parameter, and this parameter can only be a View (the view that was clicked). The method must not be private. Two different methods cannot handle the same view.

Usage example:

@Click(R.id.myButton)
void myButtonWasClicked() {
    [...]
}
@Click
void anotherButton() {
    [...]
}
@Click
void yetAnotherButton(View clickedView) {
    [...]
}

Other events

AndroidAnnotations supports other kind of events, and it works exactly as with @Click.

Currently, AndroidAnnotations supports the following events on views:

AdapterView events

You can bind methods to handle events on items in an AdapterView:

@ItemClick and @LongItemClick may have one parameter. This parameter can be of any type, it's the object retrieved when calling adapter.getItem(position).

@ItemSelect may have one or two parameters. The first parameter must be a boolean, and the second is the object from the adapter, at the selected position.

@EActivity(R.layout.my_list)
public class MyListActivity extends Activity {

	// ...

	@ItemClick
	public void myListItemClicked(MyItem clickedItem) {
	
	}
	
	@ItemLongClick
	public void myListItemLongClicked(MyItem clickedItem) {
	
	}

	@ItemSelect
	public void myListItemSelected(boolean selected, MyItem selectedItem) {
	
	}

}

Since AndroidAnnotations 2.4

For @ItemClick, @LongItemClick and @ItemSelect, if the parameter is of type int, then the position is given instead of the object coming from the adapter.

@EActivity(R.layout.my_list)
public class MyListActivity extends Activity {

	// ...

	@ItemClick
	public void myListItemClicked(int position) {
	
	}
	
	@ItemLongClick
	public void myListItemLongClicked(int position) {
	
	}

	@ItemSelect
	public void myListItemSelected(boolean selected, int position) {
	
	}

}

Wiki

Using AndroidAnnotations

Questions?

Developing AndroidAnnotations

Misc

Clone this wiki locally