Blog purpose for android basic example for android app developer. any query please my contact

Wednesday, 1 April 2020

android list view item selector example

Today learn android list-view item selector example. to basically android provides a listview a create an array then a Delaire String from the ArrayList

To display a list, you can include a list view in your layout XML file


A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead, list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down.


android list view follows the OnItemClickListener code:

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the selected item text from ListView
                String selectedItem = (String) parent.getItemAtPosition(position);

                // Display the selected item text on TextView
                tv.setText("Your favorite : " + selectedItem);
            }
        });
    }


Follows the android list-view item selector example Step by step code:



1.MainActivity.java:


package com.akash.selectlist;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends Activity {

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

        // Get reference of widgets from XML layout
        ListView lv = (ListView) findViewById(R.id.list_fruit);
        final TextView tv = (TextView) findViewById(R.id.text1);

        // Initializing a new String Array
        String[] fruits = new String[] {
                "Mango",
                "Orange",
                "Pinaple",
                "Malay Apple ",
                "Mamoncillo",
                "Persian lime"
        };

        // Create a List from String Array elements
        List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));

        // Create a ArrayAdapter from List
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, fruits_list);

        // Populate ListView with items from ArrayAdapter
        lv.setAdapter(arrayAdapter);

        // Set an item click listener for ListView
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the selected item text from ListView
                String selectedItem = (String) parent.getItemAtPosition(position);

                // Display the selected item text on TextView
                tv.setText("Your favorite : " + selectedItem);
            }
        });
    }
}


2.activity_main.xml:


<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#e8d25b"
    >
    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textColor="#5f65ff"
        android:background="#d5daff"
        android:padding="5dp"
        android:text="Choose your most favorite."
        />
    <ListView
        android:id="@+id/list_fruit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        />
</RelativeLayout>



3.strings.xml:

<resources>
    <string name="app_name">Select list</string>
</resources>


4.AndroidManifest.xml:


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

    <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>


5.Output:







No comments:

Post a Comment