Android Tutorials Part-6: Android Event Listeners

Last updated on Mar 15,2023 11.9K Views

Android Tutorials Part-6: Android Event Listeners

edureka.co

Much has already been covered in our previous Android tutorials about Android system’s basic components. Let us end the year with concepts which are a little more advanced. This Android Tutorial explains a 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 but a few are more frequently. In this Android tutorial we shall stick only to the basic ones.

What are the most commonly used Listeners?

The following basic Listeners are the most commonly used:

Let us get into the details of each.

1. OnClickListener

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

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


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).


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.

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.

Happy Learning!

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

You may also like these related posts:

For details, You can even check various concepts related to Android app development, and will help you become a certified Android developer with the Android development course.

BROWSE COURSES