articles

Home / DeveloperSection / Articles / Listview in Android

Listview in Android

Nitish Kesarwani 2741 26-Dec-2017

ListView

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 the list. ListView, TextView 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 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 reference in the ListView

Methods

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

 smoothScrollByOffset(): It provides smooth scrolling 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

Listview in Android


Leave Comment

Comments

Liked By