Skip to content
This repository was archived by the owner on Jun 8, 2025. It is now read-only.

Event Listener Binding

Steven Lewi edited this page Jul 13, 2015 · 5 revisions

You can tag any method in your class as an event listener using annotation. Some of the annotations require the method to have mandatory parameters. This is the list of supported event.

@OnClick : Used in any kind of View
@OnClick(R.id.btn1)
public void button1Clicked() {
    
}
@OnLongClick : Used in any kind of View
@OnLongClick(R.id.btn1)
public void button1LongClicked() {
    
}
@OnFocusChange : Commonly used in EditText
@OnFocusChange(R.id.edit_text1)
public void editText1FocusChanged(boolean hasFocus) {
    // Method that tagged using @OnFocusChange needs to have 1 boolean parameter.
}
@OnTouch : Used in any kind of View
@OnTouch(R.id.view1)
public void onViewTouched(MotionEvent event) {
    // Method that tagged using @OnTouch needs to have 1 MotionEvent parameter.
}
@OnItemClick : Used in AdapterView (ListView, GridView, etc)
@OnItemClick(R.id.list_view1)
public void listViewItemClicked(int position) {
    // Method that tagged using @OnItemClick needs to have 1 int parameter.
}
@OnItemLongClick : Used in AdapterView (ListView, GridView, etc)
@OnItemLongClick(R.id.list_view1)
public void listViewItemLongClicked(int position) {
    // Method that tagged using @OnItemLongClick needs to have 1 int parameter.
}
@OnItemSelected: Commonly used in Spinner
@OnItemSelected(R.id.spinner1)
public void spinner1ItemSelected(int position) {
    // Method that tagged using @OnItemSelected needs to have 1 int parameter.
}
@OnCheckedChanged: Commonly used in CompoundButton (CheckBox, RadioButton, Toggle, etc)
@OnCheckedChanged(R.id.checkbox1)
public void checkBox1CheckedChanged(boolean checked) {
    // Method that tagged using @OnCheckedChanged needs to have 1 boolean parameter.
}