Android - List view - setOnItemClickListener

Android - List view - setOnItemClickListener - In this series, we are going to learn about the click Listeners in android and how to use them?
Submitted by Manu Jemini, on January 26, 2018

Below is an example of how to use this technique in List Items. To show that this works, I have created a simple ListView which have a text view.

Android - SetOnItemClickListener 1

Image source: https://www.android-examples.com/wp-content/uploads/2016/02/after-delete.png

Here, if a user presses the Item in the list. So how this is works? We have implemented a function which will be called whenever the button gets hits.

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	@Override
	public void onItemClick(AdapterView <?> parent, View view, int position, long id) {
		Toast.makeText(MainActivity.this, items.get(position)+"", Toast.LENGTH_SHORT).show();
	}
});

With the above function, you need to put your code inside onItemClick block.

Java file:

package com.example.vikramdiwakar.myapplication;
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.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView list;
    ArrayList<String> items;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_list);

        list = (ListView) findViewById(R.id.list);
        items = new ArrayList<String>();
        items.add("ABHISHEK SHARMA");
        items.add("MANU JEMINI");
        ArrayAdapter<String> itemsAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

        list.setAdapter(itemsAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, items.get(position)+"", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

XML file:

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

Menifest file:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Output

Android - setOnItemClickListener

Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.