Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to use ImageView as a Button in Android?

  • Difficulty Level : Expert
  • Last Updated : 11 Feb, 2021

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. 

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
      
    <!--ImageView which will used as a button 
        to switch from one activity to another-->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:srcCompat="@drawable/geeksforgeeks" />
      
</RelativeLayout>

Step 4: Working with the MainActivity.java file

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

Java




import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
      
    ImageView imageView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // initialize imageView 
        // with method findViewById()
        imageView = findViewById(R.id.imageView);
  
        // Apply OnClickListener  to imageView to 
        // switch from one activity to another
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            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);
                startActivity(intent);
            }
        });
    }
}

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

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!