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.