How to use Listview in android studio step by step? android tutorial

In this Android tutorial, we will learn how to use listview in the android studio? We will build an app. This app will contain a list of countries and when someone clicks on any country, a Toast message will appear on the screen. So create you blank project.

Useful android tutorials

  • brief overview of project files in android studio
  • Basic overview of android studio (Hello world)
  • How to use button in android studio?

    Edit MainActivity.xml file

    Add listview in MainActivity.xml and give as id to it. We need only Listview and nothing else.

    <ListView
        android:id="@+id/listview"
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.0" />

    Edit Strings.xml

    Now open Strings.xml. In this file we will add list of countries. We are using <string-array name=”array”> </string-array> tag and <item> tag to enter the full list of countries. See the String.xml file code in the following block.

    
    
    <resources>
        <string name="app_name">My Application</string>
    
    
        <string-array name="array">
            <item>Afghanistan</item>
            <item>Angola</item>
            <item>Austria</item>
            <item>Azerbaijan</item>
            <item>America</item>
            <item>Australia</item>
            <item>Bhutan</item>
            <item>Bolivia</item>
            <item>Brazil</item>
            <item>Cambodia</item>
            <item>Cameroon</item>
    
            <item>Canada</item>
            <item>Chad</item>
            <item>Chile</item>
            <item>Congo</item>
            <item>Cuba</item>
            <item>Cyprus</item>
            <item>Denmark</item>
            <item>Bulgaria</item>
    
            <item>Djibouti</item>
    
            <item>Denmark</item>
            <item> Egypt</item>
            <item>Estonia</item>
            <item>Fiji</item>
    
            <item>India</item>
        </string-array>
    </resources>
    [\xml]

    Edit MainActivity.java

    It’s  obvious that in this file we will add java code. This java code is not complex. It’s easy and simple.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package com.exampel.gaurav.myapplication;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    public class MainActivity extends AppCompatActivity {
    ListView listView;
    String[] array;
    ArrayAdapter&amp;amp;amp;lt;String&amp;amp;amp;gt; arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView)findViewById(R.id.listview);
    array = getResources().getStringArray(R.array.array);
    arrayAdapter = new ArrayAdapter&amp;amp;amp;lt;String&amp;amp;amp;gt;(this,android.R.layout.simple_list_item_1,array);
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView&amp;amp;amp;lt;?&amp;amp;amp;gt; parent, View view, int position, long id) {
    Toast.makeText(getApplicationContext(),"you clicked on :" +arrayAdapter.getItem(position),Toast.LENGTH_LONG).show();
    }
    });
    }
    }

    In this code, we made instances of Listview and Arrayadapter. To access the data from strings.xml , we made a array and access it by using getResources method and pass string array name in getStringArray(R.array.array). Then in Arrayadapter it takes 3 arguments. First is this. Second is xml layout style. Here you can make your own xml also but you can use builtin file also. This file will decide how the Listview will look like. In this case we have used builtin file  android.R.layout.simple_list_item_1.  The last argument is array name which is array in this case.

Leave a Reply

Your email address will not be published. Required fields are marked *