How to use ImageView as a Button in Android?
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.
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.
That’s all, now the application is ready to install on the device. Here is what the output of the application looks like.