Event Handling in Android
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.
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
Event Handler | Event Listener & Description |
---|---|
onClick() | OnClickListener()
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.
|
onLongClick() | OnLongClickListener()
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.
|
onFocusChange() | OnFocusChangeListener()
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.
|
onKey() | OnFocusChangeListener()
This is called when the user is focused on the item and presses or releases a hardware key on the device. You will use onKey() event handler to handle such event.
|
onTouch() | OnTouchListener()
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.
|
onMenuItemClick() | OnMenuItemClickListener()
This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.
|
onCreateContextMenu() | onCreateContextMenuItemListener()
This is called when the context menu is being built(as the result of a sustained "long click)
|
Example of Button Click and LongClick
This example includes
Event Handling in android
Event Listener and call back method in android
Add Multiple Event Listener in android
Start project
Create blank Activity named as Event2Activity
Button and textview placed on Activity window.
Event2Activity.java File
package com.example.root.mysecondapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Event2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event2); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); Button btnNewClick = (Button) findViewById(R.id.btmNewClick); btnNewClick.setOnClickListener( new Button.OnClickListener(){ public void onClick(View v){ TextView txtHello = (TextView) findViewById(R.id.txtHello); txtHello.setText("Hello Shaneesh"); } } ); btnNewClick.setOnLongClickListener( new Button.OnLongClickListener(){ public boolean onLongClick(View v){ TextView txtHello = (TextView) findViewById(R.id.txtHello); txtHello.setText("Long Press Clicked!"); return true; } } ); } }
Touch Mode
Users can interact with their devices by using hardware keys or buttons or touching the screen.Touching the screen puts the device into touch mode. The user can then interact with it by touching the on-screen virtual buttons, images, etc.You can check if the device is in touch mode by calling the View class’s isInTouchMode() method.
No comments:
Post a Comment