Android List View Example

You have learned many other layouts of Android, this tutorial explains list view in android with example.

ListView

Do you want to display a list in your app? Android has its solution by providing list view layout. It is a layout which displays items in a vertical scroll-able list. Each item in the list is positioned below to the previous item of the list. List items are stored in an array and inserted to the list by using adapter which pulls items from array. List view is sub class of AdapterView.  Custom lists are very common in mobile application and list view provides a very easy way to create custom lists. It is one of the most used layouts, for example when you want to display a group of date, nothing could be more suitable than a list. Following figure shows how list view looks

list view

list view

Attributes of List View

Here are some important XML attributes of list view.

android: divider, it is used as a draw able or color to draw between list items.

android: entries, it is used to reference an array resource to populate the list view.

android: headerDividersEnabled, used to draw divider after each header views.

android: footerDividersEanabled, used to draw divider before each footer views.

Methods of List View

List view has many public methods some of them are explained below.

  • getAdapter( ): It returns adapter used in list view.
  • addHeaderView( ): It is used to add a header view at the top of list.
  • getAccessibilityClassName( ): It returns the class name of object.
  • getDivider( ): returns the divider between each item of the list.
  • getDividerHeight( ): returns divider height.
  • isOpaque( ): Shows whether list is opaque or not.
  • removeFooterView( View view): It is used to remove previously added footer view in the list.
  • removeHeaderView( View view): It is used to remove previously added header view in the list.

Constructors of List View

List view has four different public constructors as written below:

  • ListView( Context context )
  • ListView( Context context, AttributeSet attrs )
  • ListView( Context context, AttributeSet attrs, int defStyleAttr )
  • ListView( Context context, AttributeSet attrs, AttributeSet attrs, int defStyleRes )


(adsbygoogle = window.adsbygoogle || []).push({});

Example of List View

Here is an example of using list view in Android. It shows list of languages in computer science. Let’s start it by creating an activity, open activity _main.xml and paste this code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >

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

</LinearLayout>

Create another layout as activity_listview.xml and paste this code

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

open your MainActivity.java and use this code

package com.example.admin.listviewexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    String[] mobileArray = {"Java","C++","C#","CSS",
            "HTML","XML",".Net","VisualBasic", "SQL", "Python", "PHP"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}

Here is the output how it looks like

list view example

list view example

You can download this example code by following this link.

5 1 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments