ListView

ListView is basically used when you have to show items in a vertically scrolling list. One of the best examples of it is our device’s Contact List which can be With ListView and the user can easily browse the required information, while scrolling up and down also we can divide between every item and set its height and color as per your UI design.

Inside a ListView we will show a list of Text items by using TextView and pictures using ImageView also any other view or a combination of views.

As ListView is basically used to display a large set of data, so it can not feasible to manually create list items for the complete data, so Android provides us with special Adapter classes that can be used to supply data from datasets to ListView.

Given below are some of the main attributes which are most commonly used:

Attribute Description
android:divider Bt using this attribute we can specify a divider between List items and drawable or any color can be specified as a value for this attribute.
android:dividerHeight Used to specify height of the divider of the list item.

As given below we have shown how we can add a ListView to your android application using the layout XML.


<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@android:color/black"
    android:dividerHeight="1dp"/>

Using Adapter with ListView

Let’s see how you can use an Adapter to read data from an array so it will display it in the form of a List.

We will define a ListView in the main layout XML file activity_main.xml in layout screen.


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFEB3B">
    
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/black"
        android:dividerHeight="1dp"/>
        
</android.support.constraint.ConstraintLayout>

So by using this, we have to define a ListView to be created in our MainActivity class.
So we need a data and a View into which the dataset will be converted by the Adapter format.

Here we have a simple Array with festivals names in it:


String[] festivals = {
    "Diwali",
    "Holi",
    "Christmas",
    "Eid",
    "Baisakhi",
    "Halloween"
};

As your data set has simple text values, so we can define a simple TextView to hold these values or populate the ListView. Does it sound confusing? Let it sink in.

If your dataset would have had an image and some text along with it and then we can also define a TextView along with an ImageView to display the data in the List.

So now we will create a new XML file with name list_item.xml in the layout folder, a TextView in it like this.


<?xml version="1.0" encoding="utf-8"?> 
  
<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/textView"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"
    android:textStyle="bold" 
    android:layout_marginLeft="10dp"  
    android:layout_marginTop="5dp"  
    android:padding="4dp"  
    android:textColor="#000000"  
    />

Now its time for the finale, below we have the MainActivity.java class, in which we have used an ArrayAdapter to create text views from the data in the array, and create a list by supplying those view objects to the ListView.


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.ListView;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class MainActivity extends AppCompatActivity {  
    ListView listView;  
    TextView textView;  
    String[] festivals = {
        "Diwali",
        "Holi",
        "Christmas",
        "Eid",
        "Baisakhi",
        "Halloween"
    };
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        listView = (ListView)findViewById(R.id.listView);  
        textView = (TextView)findViewById(R.id.textView);  
         
        final ArrayAdapter adapter = new ArrayAdapter(this,  
                R.layout.list_item, android.R.id.textView, festivals);  
                
        listView.setAdapter(adapter);  
  
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {  
                // TODO Auto-generated method stub  
                
                /* appending Happy with festival name */
                String value = "Happy " + adapter.getItem(position);  
                /* Display the Toast */
                Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();  
            }  
        });  
    }  
}

Subscribe Now