How to use OnClickListener in android

In android application development  we can use OnClickListener in two ways. In first method is add OnClickListenerto each button individually. Imagine that you have several buttons like below image.

Now you want to use OnClickListener function in each button. Code snippet can be shown as

package com.lister;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class Main extends Activity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1);
        btn1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast pieceToast= Toast.makeText(getApplicationContext(), "Image Button One Clicked", Toast.LENGTH_SHORT);
				pieceToast.show();		

			}
		});

        ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
        btn2.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast pieceToast= Toast.makeText(getApplicationContext(), "Image Button Two Clicked", Toast.LENGTH_SHORT);
				pieceToast.show();		

			}
		});

        ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3);
        btn3.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast pieceToast= Toast.makeText(getApplicationContext(), "Image Button Three Clicked", Toast.LENGTH_SHORT);
				pieceToast.show();		

			}
		});

        ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4);

        ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5);

    	}

	}

As my example you can see you want to use OnClickListener in each and every button you have to implement lot of codes. But when you implement OnClickListener interface to your Action class then you can minimize your code like below

package com.lister;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1);
        btn1.setOnClickListener(this);

        ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
        btn2.setOnClickListener(this);

        ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3);
        btn3.setOnClickListener(this);

        ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4);
        btn4.setOnClickListener(this);

        ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5);
        btn5.setOnClickListener(this);

    }

	public void onClick(View v) {

		Toast pieceToast=null;

		switch (v.getId()) {

		case R.id.imageButton1:
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button One Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();			
			break;

		case R.id.imageButton2:
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button Two Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();
			break;

		case R.id.imageButton3:
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button Three Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();
			break;

		case R.id.imageButton4:			
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button Four Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();
			break;

		default:
			break;
		}

	}

	}

This is very good practice when we are more button in our application. Not only image button you can use any GUI widget and use this technique when you are coding your Android application.

6 thoughts on “How to use OnClickListener in android

  1. Pingback: How to use OnClickListener in android | soufianeneffar

  2. Check out my blog. The following post includes a video that shows you how to create an Android smartphone application that displays a vertically scrolling list of countries on the screen. The application is created using the Eclipse IDE and Android SDK. It features an “OK” button at the bottom of the screen that, when pressed, terminates the application.

    In the process of making this Android application you will encounter the following SDK object types, etc:
    • ScrollView,
    • Button,
    • View,
    • TableLayout,
    • TableRow,
    • OnClickListener,
    • TextView,
    • Colours in Android represented by hexadecimal numbers.

    http://androidprogrammeringcorner.blogspot.com/2015/03/pak-longs-android-programming-corner.html

    Best regards,

    Philip

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s