askIT

Android ImageButton listener OnClick

You may have a image that you want to record a click on. When the image is clicked you want to do some code. This can be achived by using the listener code.

Below is a example of using a listner on a ImageButton.

/* ImageButton listener */
ImageButton imageButtonRecord = (ImageButton)findViewById(R.id.imageButtonRecord);
imageButtonRecord.setOnClickListener(new View.OnClickListener() {
        @Override
                public void onClick(View v) {
                        // My code here
                }
        });

Example

Let's create a simple application changes a microphone icon from when a ImageButton is clicked.

exampl1.png exampl1.png

Download icons
First step is to download the icons into
"app -> res -> drawable".

microphone_grey.png microphone_red.png

Add imagebutton
Go to your design file "activity_main.xml".

01_add_imagebutton.jpg

Give it the ID "imageButtonRecord".

02_give_id.jpg

Create code in MainActivity
Import the following classes in MainActivity.

import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

The code below shall be placed inside the onCreate method in MainActivity.

/* ImageButton Username listener */
ImageButton imageButtonRecord = (ImageButton)findViewById(R.id.imageButtonRecord);

imageButtonRecord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        // My code here
                        imageButtonRecordClicked();
                }
        });

We now need to code the void method imageButtonRecordClicked() in MainActivity.

        void imageButtonRecordClicked(){
                Toast.makeText(this, "Recording", Toast.LENGTH_SHORT).show();
                ImageButton imageButtonRecord = (ImageButton)findViewById(R.id.imageButtonRecord);
                imageButtonRecord.setImageResource(R.drawable.microphone_red);
        }

Full code
Below is the full code.

package com.nettport.imagebuttonclick;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);


                /* ImageButton listener */
                ImageButton imageButtonRecord = (ImageButton)findViewById(R.id.imageButtonRecord);

                imageButtonRecord.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                // My code here
                                imageButtonRecordClicked();
                        }

                });
        }

        void imageButtonRecordClicked(){
                Toast.makeText(this, "Recording", Toast.LENGTH_SHORT).show();
                ImageButton imageButtonRecord = (ImageButton)findViewById(R.id.imageButtonRecord);
                imageButtonRecord.setImageResource(R.drawable.microphone_red);
        }
}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.