
Events are a useful way to collect data about a user’s interaction with interactive components of Applications. Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis. You can capture these events in your program and take appropriate action as per requirements.
There are following three concepts related to Android Event Management −
Event Listeners − An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.
Event Listeners Registration − Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event.
Event Handlers − When an event happens and we have registered an event listener for the event, the event listener calls the Event Handlers, which is the method that actually handles the event.
Event Listeners & Event Handlers
onClick()
This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. You will use onClick() event handler to handle such event. Listener is OnClickListener()
1 2 3 4 5 6 7 8 |
b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //todo } }); |
onLongClick()
This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event. Listener is OnLongClickListener()
1 2 3 4 5 6 |
tv.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { //todo } }); |
onFocusChange()
This is called when the widget looses its focus ie. user goes away from the view item. You will use onFocusChange() event handler to handle such event. Listener is OnFocusChangeListener()
1 2 3 4 5 6 7 8 |
bt.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { //todo } }); |
onTouch()
This is called when the user presses the key, releases the key, or any movement gesture on the screen. You will use onTouch() event handler to handle such event. Listener is OnTouchListener()
1 2 3 4 5 6 7 8 9 |
imageButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { //todo } }); |
onMenuItemClick()
This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event. Listener is OnMenuItemClickListener()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } |
onCreateContextMenu()
This is called when the context menu is being built(as the result of a sustained “long click) Listener is onCreateContextMenuItemListener()
1 2 3 4 |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, v.getId(), 0, "Something"); menu.add(0, v.getId(), 0, "Something else"); } |
onRatingBarChange
1 2 3 4 5 6 7 8 9 10 |
rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // Do what you want } } }); |
onCheckedChange
1 2 3 4 5 6 7 8 |
checkBox = (CheckBox)v.findViewById(R.id.approved_checkbox); checkBox.setChecked(true); checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // update your model (or other business logic) based on isChecked } }); |
OnItemClick
1 2 3 4 5 6 7 8 |
lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) { Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); |
onItemSelected
1 2 3 4 5 6 7 8 9 10 11 |
@Override public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } |