How to implement simple ListView in Android

Here I am going to show you how to implement simple ListView in android. First of all, you need to know what is list view and how it works in android then we will move towards the implementation.

What is ListView :

List view is a list of items having the same view or you can say the presentation. In android, Adapter is used to hold the list item which then binds items to the listview. Adapter fetches the data from the data source like the array or from the cursor which stores the result of the database query. Now we will see the actual implementation of a list view.

Implement simple ListView in android :

To implement simple listview in android follow the below steps :

  1. Create the new project in Android Studio as File ->New-> New Project.
  2. Name your project whatever you want and rename MainActivity as SimpleListViewActivity then select Finish.
  3. We want data that will display in list view, here I am using an array as a data source. Create string array inside res -> values-> string.xml as below.
<resources>
    <string name="app_name">ListViewExample</string>

    <string-array name="types_of_os">
        <item>Android OS (Google Inc.)</item>
        <item>Bada (Samsung Electronics)</item>
        <item>BlackBerry OS (Research In Motion)</item>
        <item>iPhone OS / iOS (Apple)</item>
        <item>MeeGo OS (Nokia and Intel)</item>
        <item>Palm OS (Garnet OS)</item>
        <item>Windows Mobile (Windows Phone 7)</item>
    </string-array>
</resources>

4. Create the layout for the single item of a list. res » layout » right click » Layout resource file » give a name listview_item.xml  then press OK.

5. Copy the below code to the listview_item.xml.

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/osName"
        android:padding="15dp"
        android:layout_marginTop="10dp"
        android:textSize="18dp"
        android:background="@android:color/holo_green_light"/>

</LinearLayout>

6. Now open SimpleListViewActivity from java package and copy the below text.

package com.buzzmycode.listviewexample;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class SampleListViewActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] mobile_os = getResources().getStringArray(R.array.types_of_os);
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.listview_item, R.id.osName, mobile_os));

    }
}

7. Finally, Open your AndroidManifest.xml from a manifest package and add make changes to your AndroidManifest file as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.buzzmycode.listviewexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SampleListViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

8. Now run your project you will see the output as shown below.

How to implement simple ListView in android
Simple ListView in Android

I hope this would have helped you. For any query you can comment below, I am always ready to help you.