Android Tutorials Part-6: Android Event Listeners

Recommended by 27 users

Dec 31, 2012
Android Tutorials Part-6: Android Event Listeners
Add to Bookmark Email this Post 4.5K    4

Much has already been covered in our previous Android tutorials about Android system’s basic components. Let us end the year with concepts a little more advanced. This Android Tutorial is dedicated to the very important part of Android that is Event Listeners.

Android Tutorial: What are Listeners?

Android Listeners are used to capture events. When, for instance, the user interacts with the Android system by clicking on a button, the Listeners would prompt the underlying activity to do the task associated with the button click.

Therefore, whenever you want the Android system to perform a task (when an event occurs), Listeners are at work. In the Android system, there are more than 20 Listeners. However, a few are used more than the others. In this Android tutorial we shall stick only to the basic ones.

Know-more-Android

What are the most commonly used Listeners?

The following basic Listeners are the most commonly used:

  • OnClickListener
  • OnTouchListener
  • OnDateSetListener

Let us get into the details of each.

1. OnClickListener

OnClickListener is used when one wants to execute a task on the event of the click.

Example: On the click of the button, a toast will display the number of clicks.

Android-Tutorials-Android-listener-exampleAndroid-tutorials-Event-Listener

Here’s the code for this example:

a) Declare a variable Globally outside the onCreate method:

//Declaring the variable to count the number of clicks
			int count = 0;

b) In the onCreate method implement the following. Here, we are setting an OnClickListener on a Button:

// Declaring the Button and Type Casting it.
		Button btn1 = (Button) findViewById(R.id.button1);

		// Setting OnClickListener on btn1
		btn1.setOnClickListener(new OnClickListener() {

			@Override
		public void onClick(View v) {
			//Executing the following code on the click

//Incrementing the variable count by 1 on the click of the button
			count++;

//Displaying the count using a Toast Toast.makeText(MainActivity.this,
"The button has been clicked " + count +     " times",Toast.LENGTH_SHORT).show();
		}
	});

2. Android OnTouchListener

OnTouchListener is used to capture a touch event and execute the task associated with it.

Example : On button touch, a toast will be displayed with the count (the number of times the touch event occurs).

Android-tutorials-OnTouch-ListenersOnTouch-Listeners-Example-Android-Tutorial-Part-6

Here’s the code for the above example:

a) Declare a variable Globally outside the onCreate method :

//Declaring the variable to count the number of clicks
			int count = 0;

b) Implement the following in the onCreate method. Here we are setting a OnTouchListener on a Button:

 // Declaring the Button and Type Casting it.
		Button btn1 = (Button) findViewById(R.id.button1);

		//Setting OnTouchListener on btn1
		btn1.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
			//Incrementing the variable count by 1 on every Touch
				count++;
				Toast.makeText(MainActivity.this,
"The button has been touched " + count  + " times",Toast.LENGTH_SHORT).show();

				return false;
			}
		});

3. OnDateSetListener

It is used to fetch the selected date from a DatePicker widget.

Example :  On button click a DatePickerDialog appears from which you can select a date.
After making the selection, and clicking the Done button, a toast will appear with the selected date.

Android-tutorials-for-beginners-OnDateset-Listeners

Here’s the code for the above example:

//Declaring a button and the DatePickerDialog
private Button btnDatePicker;
	DatePickerDialog _date;

Implement the following in the onCreate method. Here we are setting an OnClickListener on a Button:

//Typecasting the Button
	btnDatePicker = (Button) findViewById(R.id.button1);

	//Setting an OnclickListener on the Button
	btnDatePicker.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {

//Creating an object of DatePickerDialog incontext of the Mainactivity
		//dateCallback is called which defined below
_date = new DatePickerDialog(MainActivity.this, dateCallback,     	           2012, 10, 12);
		//Showing the DatePickerDialog
			_date.show();
		   }
	    });

	//Setting OnDateSetListener on the DatePickerDialog
private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener() {

		@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {

//Displaying a Toast with the date selected
Toast.makeText(MainActivity.this, "The date is : " + dayOfMonth+"/"+  ++monthOfYear +"/"+  year, Toast.LENGTH_LONG).show();
		}
	};

We hope you enjoyed this tutorial about Android Listeners. You can download all the sample code from this Android tutorial here!

[dl url=”#” class=”eModal eModal-8″  title=”Download Code” desc=”” type=”” align=”” for=”vide0″

 

Happy Learning!

Following resources were used in creating this Android Tutorial: developer.android.comEdureka.in

You may also like these related posts:

Share on
Comments
4 Comments
  • Carrol Gitting

    very good put up, i definitely love this web site, keep on it

24 X 7 Customer Support X

  • us flag 1-800-275-9730 (Toll Free)
  • india flag +91 88808 62004