Tuesday, September 7, 2010

Event Handling in Android

There are many events in android which can be handle through code but I am currently go with onClickListener.
As you know that all events are listen through a listener and we will work with OnClickListener.
You have a button in xml layout and you are accessing it with the code:
       Button myBtn = (Button) findViewById(R.id.my_button);
Now we are register a listener with it as
        myBtn.setOnClickListener(new MyListener ());//Object_of_OnClickListener_class

Now I am going to develop a class which will be implement OnClickListener which is member of android.view.View.OnClickListener.

Import android.view.View.OnClickListener;

public class MyListener implements OnClickListener{
   public void onClick(View v) {
         //your action
   }
}

Now write your logic for your action.

Practice it and let me know if you have any problem.

No comments:

Post a Comment