android listview

Android ListView Tutorial with Example using Android Studio

Vamsi Tallapudi Android, Programming, Tutorials, Tutorials 9 Comments

Android ListView Tutorial with Example using Android Studio:

Note: Android RecyclerView is the most advanced and recommended version of ListView. You can refer to the article on Android RecyclerView here.

In this Article we are going to discuss about how to implement a simple Android ListView with example using Android Studio. In this example we will create and display a list using default android Adapter and we display another activity when any item on the list is tapped.android listview tutorial with example

Our main goal here is to display various Country names in the listview and when the user touches any Country name, a new Activity starts displaying that Country’s flag. The final output of the code looks as shown in the following video:

Let us dive into the tutorial.

Steps to create a simple Android ListView using Android Studio:

In this Simple Android ListView project, we need two activities – one to display the android listview and the other to display the country flag. Let us first start by creating the project.

  1. Create a New Android Studio Project by opening File > Close Projects > Start a New Android Studio Project and giving an appropriate Application name. I named the Application Name as Simple ListView. Select Blank Activity and hit Next and select Activity name. I kept mine as default – Now hit Finish.
  2. Now Open activity_main.xml (or the xml created along with your Activity) and add the following code:
    This code is pretty much basic – we just added an android listview to the default Relative Layout.
  3. Next we need the country flags to display accordingly. You can use the flags provided from this project’s zip file attached to this tutorial or use your own images. Place them in your Res> Drawable folder.
  4. Now we open MainActivity.java and we wire up the android listview in xml file. We add the code as follows:
    The code is commented whereever it is necessary. Here we discuss about it in detail. After inflating the layout in Line 34, we set up a reference variable mListView and wire it up with listview. Thereafter we declare an Array Adapter. This adapter takes three parameters -context, resource and array of objects. This adapter acts as a bridge between the list and adapterview. We next declare an Intent to Start second Activity which I’ve named as CountryActivity.java. Now we pass the value of the resource value based on the item clicked to the next activity via putExtra() method of intent. Next we start another activity using startActivity(intent) method.

  5. Now we need to create another activity which will be shown when user clicks an item from the android listview. We can create a new Activity by Right clicking on the package name and selecting New>Activity>Blank Activity. I’ve named my new activity as CountryActivity.java  and the corresponding xml as activity_country.xml.
  6. Now let us modify activity_country.xml first. We need to display the corresponding country flag in this layout. So the element we need is an ImageView which will be filled according to the country the user pressed. Now we modify the activity_country.xml as follows:
  7. Now let’s wire up this ImageView element to the java class. We open CountryActivity.java and type the following code:
    Here we obtained the reference to the imageview in line 17 and we also retrieved the resource data in line 20 from the intent by providing the key “flag”. Now we set the imageview in line 21 using the resource data we retrieved.

     

Now we can successfully run the Project and Display the List of Countries. If any of the country name is clicked, a new activity starts displaying the corresponding country’s flag. In the next tutorial we will discuss about custom android listview.

Related Posts