How to use ImageView as a Button in Android? - GeeksforGeeks

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.
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 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:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    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.
android
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Android Listview in Java with Example
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples