Skip to content
Related Articles
Open in App
Not now

Related Articles

How to use ImageView as a Button in Android?

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 11 Feb, 2021
Improve Article
Save Article

ImageView is used when we want to work with images or we want to display them in our application. So, this article will give you a complete idea of using an ImageView as a Button in android studio. So, without wasting further time let’s go to the article and read how we can achieve this task.

What we are going to develop in this article? 

We will be building a simple application in which we will be displaying an ImageView and when we click on that ImageView we will get into a new activity or simply we can say that we are going to use ImageView as a button to switch between different activities.  A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Create another new Activity

Now, we will create another Empty Activity (SecondActivity) to move from one activity to another by clicking ImageView. So, to create second activity, go to the android project > File >new > Activity > Empty Activity.

Step 3: Working with the activity_main.xml file

Now it’s time to design the layout of the application. So for that go-to the app > res > layout > activity_main.xml and paste the below-written code in the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--ImageView which will used as a button 
        to switch from one activity to another-->
        app:srcCompat="@drawable/geeksforgeeks" />

Step 4: Working with the file

Go to the app > java > package name > file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    protected void onCreate(Bundle savedInstanceState) {
        // initialize imageView 
        // with method findViewById()
        imageView = findViewById(;
        // Apply OnClickListener  to imageView to 
        // switch from one activity to another
        imageView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Intent class will help to go to next activity using
                // it's object named intent.
                // SecondActivty is the name of new created EmptyActivity.
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

That’s all, now the application is ready to install on the device. Here is what the output of the application looks like.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!