Android Open new Activity When GridView Item Is Clicked

Hi guy, In this tutorial we will learn how to open new activity when gridview item is clicked. We will create android studio GridView project which has Image Text and OnItemClickListener. When the items in the GridView are clicked a new activity will appear to display the item clicked. GridView item will consist of Image and Names of Various fruits and hence the new activity will display the Image and Name of the clicked fruit Item. Our expected output is shown below.

Android Open new Activity When GridView Item Is Clicked

 

    • The video below illustrate How to open new activity when gridview items are clicked

    • Let’s start coding our GridView

Creating a New GridView Project

    • Open your android studio click File => New Project and give it a name in this tutorial i have named it GridView.
    • Click next select Empty activity => finish.

Adding GridView to Our Activity

    • Open your activity_main.xml and add GridView
    • preview of activity_main file

grid view

Downloading Fruit image

    • Use the link below to download fruit images used used in this tutorial

download images

Creating Resource Layout File for holding GridView data

    • Right click layout => New => Layout Resource File and name it row_data.
    • Preview of row_data file

row data preview grid view

Creating activity to receive and display clicked item

    • Right click on Layout => New => Activity => Empty Activity => name it in this case i have named it activity_grid_item .

Receiving and displaying clicked items

    • We are going to use intents to send and receive clicked item.
    • To send data between activities through intent we use
    • To receive data through intent in android we use
    • Open your ListdataActivity, Copy and Paste the code below for receiving and displaying clicked item.

Initializing GridView and Setting GridView Data

    • Open your MainActivity.java class
    • We shall start with initializing our GridView and then set data GridView data.
    • To set grid view data we need to create custom Adapter that extends base Adapter.
    • Copy and Paste the code below in your MainActivity.java.

Running our app

    • Run your app and you will see the output below.

Android Open new Activity When GridView Item Is Clicked

    • Click on one item and another activity will appear with the name and image of item clicked

Android Open new Activity When GridView Item Is Clicked

  • That’s all if you have a question or need help please comment below

GET SOURCE CODE FOR ONLY $5