Android ListView is a view group which is used to display many items in vertical scrollable list. And the android list view items are automatically inserted to the list using an Adapter. Displaying element in a listview is mostly used in android application. The users see many items and can scroll the screen. When users click on any item, a new full screen activity may open there. In this tutorial, you will learn to implement listview in your android application and also learn to open new activity when listview item is clicked.
Here I have only focused to open new activity when a listview item is clicked with simple listview. I have posted many tutorial posts about list view and grid view like parallax header with android listview, android linear ListView example, display images in android GridView, ListView with image and text and many more.
Android Example: How to Open New Activity when ListView Items Are Clicked
Let’s start by creating a new android project with project name: Android ListView Items Clicked. Following are the simple steps to open new activity/page when android listview item is clicked.
XML Layout File
Open your XML Layout file and add a ListView inside Linearlayout. Following is the complete content of listview_item_click_event.xml file.
Create two new XML layout files list_item_activity_1.xml and list_item_activity_2.xml and add a TextView inside LinearLayout or any other layout with different text in textview of two layouts. These files will look like below.
Java Activity File
Create two new java file ListItemActivity1.java and ListItemActivity2.java and set the content view to the above XML layout file. Following is the complete java code of ListItemActivity1.java and ListItemActivity2.java file.
In your main java activity file, you need to add strings value for list items, ArrayAdapter and OnItemClickListener. Following is the complete java code of main activity file.
Your application AndroidManifest.xml file will look like below.
You have done all things. Now, run your Start New Activity from Android ListView onItemClick application and click on the list item, a new page/activity will open there which will look like above screenshot.