Add OnItemClickListener to RecyclerView in android to Get Clicked Item

How to get selected value from RecyclerView and CardView using RecyclerViewAdapter  addOnItemTouchListener function.

AddOnItemTouchListener method is the most important part of RecyclerView because without the proper click listener method there is only half use of this widget. RecyclerView dose not have its own click listener method like ListView so with the use of GestureDetector, MotionEvent and Boolean function onInterceptTouchEvent() we can get the clicked item from RecyclerView. So here is the complete step by step tutorial for Add OnItemClickListener to RecyclerView in android to Get Clicked Item.

android-project-download-code-button

Note: Read below steps very carefully to add CardView and RecyclerView library inside your current project.

1. Open your project’s build.gradle ( Module : app ) file.

build-gradle

2. Please add below code inside your build.gradle ( Module : app ) file.

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.+'

3. Screenshot of build.gradle ( Module : app ) file after adding above code.

Add Import CardView RecyclerView support library in Android Studio

Here your go now both libraries is successfully imported into our project now next step is to start coding.

How to Add OnItemClickListener to RecyclerView in android to Get Clicked Item.

Code for MainActivity.java file.

package com.android_examples.recyclerviewclicklistener_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> SubjectNames;
    RecyclerView recyclerview;
    RecyclerView.LayoutManager RecyclerViewLayoutManager;
    View ChildView ;
    int RecyclerViewItemPosition ;

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

        recyclerview = (RecyclerView)findViewById(R.id.recyclerview1);

        RecyclerViewLayoutManager = new LinearLayoutManager(getApplicationContext());

        recyclerview.setLayoutManager(RecyclerViewLayoutManager);

        //Adding items to RecyclerView
        SubjectNames = new ArrayList<>();
        SubjectNames.add("ANDROID");
        SubjectNames.add("PHP");
        SubjectNames.add("BLOGGER");
        SubjectNames.add("CSS");
        SubjectNames.add("MATHS");
        SubjectNames.add("ASP.NET");
        SubjectNames.add("JAVA");
        SubjectNames.add("C++");
        SubjectNames.add("HTML");
        SubjectNames.add("PHOTOSHOP");

        RecyclerView.Adapter adapter = new RecyclerViewAdapter(SubjectNames);

        recyclerview.setAdapter(adapter);

        recyclerview.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

            GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

                @Override public boolean onSingleTapUp(MotionEvent motionEvent) {

                    return true;
                }

            });
            @Override
            public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

                ChildView = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

                if(ChildView != null && gestureDetector.onTouchEvent(motionEvent)) {

                    RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(ChildView);

                    Toast.makeText(MainActivity.this, SubjectNames.get(RecyclerViewItemPosition), Toast.LENGTH_LONG).show();
                }

                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });

    }

}

Code for RecyclerViewAdapter.java class file.

package com.android_examples.recyclerviewclicklistener_android_examplescom;

/**
 * Created by Juned on 8/14/2016.
 */
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>  {

    ArrayList<String> SubjectNames;

    View view1;

    public RecyclerViewAdapter(ArrayList<String> SubjectNames1) {

        this.SubjectNames = SubjectNames1;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        view1  = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_item, viewGroup, false);

        return new ViewHolder(view1);
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.ViewHolder Viewholder, int i) {

        Viewholder.SubjectTextView.setText(SubjectNames.get(i));
    }

    @Override
    public int getItemCount() {

        return SubjectNames.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView SubjectTextView;
        public ViewHolder(View view) {

            super(view);

            SubjectTextView = (TextView)view.findViewById(R.id.textview1);
        }
    }

}

Code for activity_main.xml 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.android_examples.recyclerviewclicklistener_android_examplescom.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:scrollbars="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</RelativeLayout>

Code for cardview_item.xml layout file.

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="6dp"
    card_view:cardElevation="6dp"
    card_view:contentPadding="7dp"
    card_view:cardBackgroundColor="#FF9800"
    >

    <TextView
        android:id="@+id/textview1"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        android:textColor="#FFF3E0"/>

</android.support.v7.widget.CardView>

Screenshots:

Add OnItemClickListener to RecyclerView in android to Get Clicked Item

RecyclerView-clickListener-2

Click here to download Add OnItemClickListener to RecyclerView in android to Get Clicked Item project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *