---
title: "Listview in Android"  
description: "It provides multiple optional menus which are appeared on the screen and one can select it of their own choice. The content list appears as defined in"  
author: "Nitish Kesarwani"  
published: 2017-12-26  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12744/listview-in-android  
category: "android apps"  
tags: ["android", "android controls", "android-view"]  
reading_time: 4 minutes  

---

# Listview in Android

## ListView

It provides [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) optional menus which are appeared on the screen and one can select it of their own choice. The content list appears as defined in the list. ListView, [TextView](https://www.mindstick.com/forum/23237/how-do-i-center-text-horizontally-and-vertically-in-a-textview-in-android) is the control that is defined in the two different XML files. ListView is defined in the Main_Activity.xml whereas TextView is declared in menuitem_Activity.[xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) and another thing is that I have created array list in the Strings.xml that are used to fetch the data through ArrayAdapter class and bind with string.SetAdapter, OnselectedItem and setOnSelectedItem method is used to invoke the ListView Item.

##### Attributes

** android: dividerHeight:** This attribute sets the height of divider

** android:footerDividerEnabled:** When its value is false, the list item(ListView) cannot enable the divider from the header

** android:headerDividersEnabled:** When its value is true, the list item(ListView) enable the divider from the header

** android: divider:** It is used to draw drawable or colors between the list items

** android: entries:** This attribute shows the array [resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) [reference](https://www.mindstick.com/forum/34619/call-by-value-and-call-by-reference) in the ListView

##### Methods

** setSelection():** Currently item is selected from the list

** smoothScrollByOffset():** It provides [smooth scrolling](https://www.mindstick.com/forum/158695/how-can-implement-smooth-scrolling-on-a-webpage-using-jquery) which is based on adapter offset position

** smoothScrollToPosition():** It allows smooth scrolling which is specified in adapter position

** getAdapter():** It is used to get the array adapter class for listView

** setAdapter():** It is used to set the array adapter class for listView

###### Android_Manifest.xml

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
```

###### Main_activity.xml

```
<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:orientation="vertical"    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"    tools:context="com.mapsample.msclient009.listviewd.MainActivity">    <ListView        android:id="@+id/addList"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
```

###### listitem_activity.xml

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/displaymenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:textColor="#22AA90"
        android:padding="5dp"/>
</LinearLayout>
```

###### Strings.xml

```
<resources>
    <string name="app_name">ListViewD</string>
    <string-array name="array_technology">
        <item name="c">C</item>
        <item name="C++">C++</item>
        <item name="java">Java</item>
        <item name="scala">Scala</item>
        <item name="fortan">Fortan</item>
        <item name="python">Python</item>
        <item name="javascript">JavaScript</item>
        <item name="C#">C#</item>
    </string-array>
</resources>
```

###### Main_Activity.java

```
package com.mapsample.msclient009.listviewd;

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 {
TextView tv;
ListView lv;
String list[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        tv =(TextView)findViewById ( R.id.displaymenu );
        lv =(ListView)findViewById ( R.id.addList );
        list = getResources ().getStringArray ( R.array.array_technology );
        final ArrayAdapter adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,android.R.id.text1,list);
        lv.setAdapter ( adapter );
        lv.setOnItemClickListener ( new AdapterView.OnItemClickListener ( ) {
            @Override
            public void onItemClick(AdapterView<?> arg, View v, int p, long id) {
              String  getVal = (String) adapter.getItem ( p );
                Toast.makeText ( getApplicationContext (),"Selected Item is :"+getVal,Toast.LENGTH_LONG ).show ();
            }
        } );
    }
}
```

##### Launch Your APP

![Listview in Android](https://www.mindstick.com/mindstickarticle/a4f28057-27e9-40e9-ab2a-520107d94834/images/30c5a42f-49f5-4a0d-93fd-71b13eb9e0a1.png)

![Listview in Android](https://www.mindstick.com/mindstickarticle/a4f28057-27e9-40e9-ab2a-520107d94834/images/674bf615-a4d1-4b31-867e-80da4b6eb8af.png)\

---

Original Source: https://www.mindstick.com/articles/12744/listview-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
