List View in Android

listview in android

List View is very important to view in the android studio. It is a subclass of AbsListView. Complete Hierarchy of ListView is given

java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup
     ↳android.widget.AdapterView<android.widget.ListAdapter>
      ↳android.widget.AbsListView
       ↳android.widget.ListView

List View displays a list of data in a vertical manner, and one of the good parts is that the list is scrollable and also support pagination. List View is widely used after recycler view in the modern android application.

Creating List View in Android Studio

Adding List View in XML

First, we need to add the list view in the XML layout and also give id so we can access it in our java file. Here is the code for adding listview in XML

 <ListView
        android:id="@+id/listview"
      
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        />

Accessing List View in .Java

After Defining in .xml we also need to access the list view in java file to attach it with Adapter. We access list view in java file by this code

 ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         listview =  findViewById (R.id.listview);
        // listview.setOnItemClickListener(this);
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("GeeksToCode");
        arrayList.add("Android");
        arrayList.add("C++");
        arrayList.add("Programming");
        arrayList.add("Java");
      
}

setOnItemClickListener is used so, that we are able to click on the list but we comment it and used it later and then we create a list with some data.

Creating Adapter and Attaching with List View

The adapter is a very important topic in android. Adapter act as a bridge between the User Interface and data source. It attaches the data to the view and that’s why it is known as an adapter. Creating adapter for list view is very easy and you can do this by this code

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         listview =  findViewById (R.id.listview);
        listview.setOnItemClickListener(this);
        ArrayList<String> arrayList = new ArrayList<>();
           arrayList.add("GeeksToCode");
        arrayList.add("Android");
        arrayList.add("C++");
        arrayList.add("Programming");
        arrayList.add("Java");

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>   (getApplicationContext(), R.layout.textview,arrayList);
        listview.setAdapter(arrayAdapter);
}

We create an array adapter of type string and pass context, a text view, and an array list to the constructor, and finally, we set the adapter to listview. Now we need to R.layout.textview, this is a layout file that listview uses to show the data.

Creating layout resource file

Goto File -> New -> Layout Resource File and add name this text view and also add this in .xml section

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:textColor="@color/colorPrimary"
    android:textSize="30dp"
    android:layout_height="wrap_content"

    />

android:textColor change the text of the textView and android:textSize changes the text size of the size of text in the listview.

Now run and see the output. It show something like this

listview in android

Attaching onClickListener in listView

Now, remove the commented line in above code and implement onClickListener like this and then override abstract method

public class MainActivity<listview> extends AppCompatActivity implements AdapterView.OnItemClickListener{
    ListView listview;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         listview =  findViewById (R.id.listview);
        listview.setOnItemClickListener(this);
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("GeeksToCode");
        arrayList.add("Android");
        arrayList.add("C++");
        arrayList.add("Programming");
        arrayList.add("Java");

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.textview,arrayList);
        listview.setAdapter(arrayAdapter);

}
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String value = (String) listview.getItemAtPosition(position);
       
        Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
    }
}

onItemClick is automatically called whenever an item is clicked in listview and in value variable we get the text present in the listview which is clicked, and then we show the text in Toast. Run this and you will see something like this

list view in android
ListVew with click listener
Tagged : /

Leave a Reply

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