Android ListView Tutorial with Example using Android Studio

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.

    The Complete code can be downloaded from below link:

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.

Vamsi Tallapudi
Connect Via

Vamsi Tallapudi

Founder and Author at Code Refer
Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook 🙂
Vamsi Tallapudi
Connect Via

Vamsi Tallapudi

Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook :)

You may also like...

  • sachin joshi

    hi
    i m trying to run this application but in CountryActivity.java give an error
    “can not resolved menu_coutry”

  • Hi sachin, Check whether menu_country.xml actually exists in res >menu folder. Also check for typos.

  • Dominic Gusah

    Hi. If I wanted a situation that when the user touches any Country it came up with a list of each state within that country, what’s the best way to do this. Would you do another List view?

  • Nav Chantha

    Dear Developer, thanks for valuable tutorial, I want to make list view and when we clic on item it open to new actvity, could you show because it didnot see your guide. Thanks in advance.

  • Gj Ramanaa

    s please update the code for beginners it hard to figure out

  • Reetu Peter

    Hi Vamsi, I was following your tutorial and noticed something. In line 47 of MainActivity.java, you have stored the value of position in a variable,’itemPosition’.But you have not used itemPosition at all…
    Is it necesary to have that line?

  • Fendi Pratama

    where is the source code?

  • Manish Kumar

    Source code is not available to download.
    Can you update it.
    Thanks.

  • Annabel Teng

    could you update it? Source code not available to download?