Android Development

Event Handling

Event handling means basically how to manage the action of Users Interaction.In android every event have an action. For example if you click the button it is an event.Then some message appear on the application we call that button click event handle by the message. 

Figure 1.7

Event Listeners: 

An event listener is an interface in the View class of Android. It has a single callback technique. This technique will be considered when the View that is enlisted with the Listener is actuated by the user’s action. 

  • onClick()  

This event occurs when the user clicks on an item on the screen in touch more or focuses on an item using a trackball or navigation-keys.  

  • onLongClick() This event occurs when a user clicks on an item or screen for more than 1 second. 
  • onFocusChange() 

This event occurs when a user navigates away from an item that was on focus. 

  • onKey()  

This event occurs when a user focuses and clicks on an item. 

  • onTouch() 

This event occurs when a user touches a particular range of an item with gestures or    simple touch or tap. 

Event Handlers: 

Event handles are the genuine techniques that have the activity that will be taken. After an event has occurred and event listeners are enlisted, event listeners call Event Handler. These are valuable to characterize some callback strategies. 

  • onKeyUp() 

The system invokes this method when a new key event occurs. 

  • onKeyDown() 

The system invokes this method when a key down event occurs. 

  • onTrackballEvent() 

The system invokes this method when a trackball motion event occurs. 

  • onTouchEvent()  

The system invokes this method when some touch event occurs. 

  • onFocusChange() 

The system invokes this method when an item gets into focus or loses focus. 

Event Listener Registration: 

Event Registration is the process in which Event Listeners are registered with Event Handlers. This is important as Handler will work only after the Listener fires an Event. 

Android Event Listener registration can be done in the following three ways: 

  1. The first method to register event listeners is by directly mentioning them in activity_main.xml. 
  1. We can also register event listeners by using Activity class that implements a listener interface. 
  1. The last method is by using an Anonymous class. 
 

public class MainActivity extends AppCompatActivity { 

   Button btn; 

   TextView tView; 

   @Override 

   protected void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_main); 

      btn = (Button)findViewById(R.id.btnClick); 

      tView = (TextView)findViewById(R.id.txtResult); 

      btn.setOnClickListener(new View.OnClickListener() { 

         @Override 

         public void onClick(View v) { 

            TextView txtView = (TextView) findViewById(R.id.textView); 

            txtView.setText("You click the button"); 

            txtView.setTextSize(25); 

            txtView.setGravity(Gravity.CENTER); 

            txtView.setTextColor(R.color.colorAccent); 

         } 

      }); 

    } 

} 

UI Controls (Prev Lesson)
(Next Lesson) Styles and Themes
Back to Android Development

No Comments

Give a comment