ListView Tutorial With Example In Android Studio

List of scrollable items can be displayed in Android using ListView. It helps you to displaying the data in the form of a scrollable list. Users can then select any list item by clicking on it. ListView is default scrollable so we do not need to use scroll View or anything else with ListView.

ListView is widely used in android applications. A very common example of ListView is your phone contact book, where you have a list of your contacts displayed in a ListView and if you click on it then user information is displayed.

Adapter: To fill the data in a ListView we simply use adapters. List items are automatically inserted to a list using an Adapter that pulls the content from a source such as an arraylist, array or database.

ListView in Android Studio: Listview is present inside Containers. From there you can drag and drop on the virtual mobile screen to create it. Alternatively you can also XML code to create it.

Here is Android ListView XML Code:

1

2

3

4

5

6

7

<ListView xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/simpleListView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

tools:context="abhiandroid.com.listexample.MainActivity">

</ListView>

Listview look in Design:

Attributes of ListView:

Lets see some different attributes of ListView which will be used while designing a custom list:

1. id: id is used to uniquely identify a ListView.

Below is the id attribute’s example code with explanation included.

1

2

3

4

5

6

<!--Id ofalist view uniquely identify it-->

<ListView

android:id="@+id/simpleListView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

2. divider: This is a drawable or color to draw between different list items.

Below is the divider example code with explanation included, where we draw red color divider between different views.

1

2

3

4

5

6

7

8

<!--Divider code inListView-->

<ListView

android:id="@+id/simpleListView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:divider="#f00"

android:dividerHeight="1dp"

/>

3. dividerHeight: This specify the height of the divider between list items. This could be in dp(density pixel),sp(scale independent pixel) or px(pixel).
In above example of divider we also set the divider height 1dp between the list items. The height should be in dp,sp or px.

4. listSelector: listSelector property is used to set the selector of the listView. It is generally orange or Sky blue color mostly but you can also define your custom color or an image as a list selector as per your design.

Below is listSelector example code with explanation includes, where list selector color is green, when you select any list item then that item’s background color is green .

1

2

3

4

5

6

7

8

<!--List Selector Code inListView-->

<ListView

android:id="@+id/simpleListView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:divider="#f00"

android:dividerHeight="1dp"

android:listSelector="#0f0"/><!--list selector ingreen color-->

Adapters Use in ListView:

An adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like as list view, grid view, spinner etc.

ListView is a subclass of AdapterView and it can be populated by binding to an Adapter, which retrieves the data from an external source and creates a View that represents each data entry.

In android commonly used adapters are:

Array Adapter
Base Adapter
Now we explain these two adapter in detail:

1.Array Adapter:

Whenever you have a list of single items which is backed by an array, you can use ArrayAdapter. For instance, list of phone contacts, countries or names.

Important Note: By default, ArrayAdapter expects a Layout with a single TextView, If you want to use more complex views means more customization in list items, please avoid ArrayAdapter and use custom adapters.

Video Tutorials

OBJECTIVE...!

I’m Rajashekar,
Core Android Developer with complimenting
skills as a developer from India.
I cherish taking up complex problems and
turning them into beautiful interfaces.
My love for decrypting the logic and structure of
coding keeps me pushing towards writing elegant
and proficient code, whether it is Android, PHP
or any other platforms.