Android - Set Listener on item of a list setOnClickListener

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.

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

All you have to do is to preserve the context of the activity and use it to setOnClickListener in the Adapter, during the creation of the View. We save the Context by passing it through the constructor

CUSTOM ADAPTER File:

package com.example.vikramdiwakar.myapplication;

/**
 * Created by Vikram Diwakar on 1/23/2018.
 */
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class Adapter extends ArrayAdapter<String> {

    //the list values in the List of type hero
    List<String> items;

    //activity context
    Context context;

    //the layout resource file for the list items
    int resource;

    //constructor initializing the values
    public Adapter(Context context, int resource, List<String> items) {
        super(context, resource, items);
        this.context = context;
        this.resource = resource;
        this.items = items;
    }

    //this will return the ListView Item as a View

    @Override
    public View getView(final int position, View convertView,  ViewGroup parent) {

        //we need to get the view of the xml for our list item
        //And for this we need a layoutinflater
        LayoutInflater layoutInflater = LayoutInflater.from(context);

        //getting the view
        View view = layoutInflater.inflate(resource, null, false);

        //getting the view elements of the list from the view
        TextView textViewTeam = (TextView) view.findViewById(R.id.textName);
        Button buttonDelete = (Button) view.findViewById(R.id.btn);

        textViewTeam.setText(items.get(position));

        final String name = items.get(position);

        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, name+"'s BUTTON CLICKED",Toast.LENGTH_LONG).show();
            }
        });
        //finally returning the view
        return view;
    }
}

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");
       
        Adapter adp = new Adapter(this, R.layout.activity_main, items);

        list.setAdapter(adp);
       
    }
}

ITEM LAYOUT File:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vikramdiwakar.myapplication.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:weightSum="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="29dp"
            android:text="This Text Here"
            android:id="@+id/textName"
            android:layout_weight="1.07" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:id="@+id/btn" />
    </LinearLayout>
</RelativeLayout>

LIST LAYOUT 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 setOnClickListener 3 Output

Related Tutorials



Comments and Discussions!

Load comments ↻





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