Advanced onClick RecyclerView using Interface | RecyclerView item Click using Interface

In this tutorial, we’re going to create a recyclerView and apply on Click on the recyclerView using Interface. This the advanced way to apply items click on the recyclerView.

Before adding the interface first create recyclerView. Here I’m not going to explain how to create a recyclerView, if you don’t know how to create a recyclerView then go to my previous tutorial on recyclerView How to create RecyclerView in Android.

And if you’re familiar with it then just follow these steps to create recyclerView or we can create your own recyclerView.

Before applying the Interface callbacks

activity_main.xml

<?xml version=”1.0" encoding=”utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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=”.MainActivity”>

<androidx.recyclerview.widget.RecyclerView

android:id=”@+id/recyclerView”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

app:layout_constraintBottom_toBottomOf=”parent”

app:layout_constraintEnd_toEndOf=”parent”

app:layout_constraintStart_toStartOf=”parent”

app:layout_constraintTop_toTopOf=”parent” />

</androidx.constraintlayout.widget.ConstraintLayout>

item_row.xml

<?xml version=”1.0" encoding=”utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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=”wrap_content”

android:padding=”5dp”

android:background=”?attr/selectableItemBackground”>

<ImageView

android:id=”@+id/imageView”

android:layout_width=”48dp”

android:layout_height=”48dp”

android:layout_marginStart=”16dp”

android:layout_marginTop=”16dp”

android:layout_marginBottom=”16dp”

app:layout_constraintBottom_toBottomOf=”parent”

app:layout_constraintStart_toStartOf=”parent”

app:layout_constraintTop_toTopOf=”parent”

app:srcCompat=”@android:drawable/btn_star_big_on” />

<TextView

android:id=”@+id/textView”

android:layout_width=”0dp”

android:layout_height=”wrap_content”

android:layout_marginStart=”16dp”

android:fontFamily=”@font/poppins_semibold”

android:text=”TextView”

android:textSize=”16sp”

android:textColor=”@android:color/black”

app:layout_constraintBottom_toTopOf=”@+id/rowCount”

app:layout_constraintEnd_toEndOf=”parent”

app:layout_constraintStart_toEndOf=”@+id/imageView”

app:layout_constraintTop_toTopOf=”parent” />

<TextView

android:id=”@+id/rowCount”

android:layout_width=”0dp”

android:layout_height=”wrap_content”

android:fontFamily=”@font/poppins_light”

android:text=”TextView”

android:textSize=”14sp”

app:layout_constraintBottom_toBottomOf=”parent”

app:layout_constraintEnd_toEndOf=”parent”

app:layout_constraintStart_toStartOf=”@+id/textView”

app:layout_constraintTop_toBottomOf=”@+id/textView” />

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerViewAdapter.java

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

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

private static final String TAG = “RecyclerAdapter”;

List<String> moviesList;

public RecyclerAdapter(List<String> moviesList) {

this.moviesList = moviesList;

}

@NonNull

@Override

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

View view = layoutInflater.inflate(R.layout.row_item, parent, false);

ViewHolder viewHolder = new ViewHolder(view);

return viewHolder;

}

@Override

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.rowCountTextView.setText(String.valueOf(position));

holder.textView.setText(moviesList.get(position));

}

@Override

public int getItemCount() {

return moviesList.size();

}

class ViewHolder extends RecyclerView.ViewHolder implement View.OnClickListener {

ImageView imageView;

TextView textView, rowCountTextView;

public ViewHolder(@NonNull View itemView) {

super(itemView);

imageView = itemView.findViewById(R.id.imageView);

textView = itemView.findViewById(R.id.textView);

rowCountTextView = itemView.findViewById(R.id.rowCount);

itemView.setOnClickListener(new View.OnClickListener(this);

itemView.setOnLongClickListener(new View.OnLongClickListener() {

@Override

public boolean onLongClick(View view) {

moviesList.remove(getAdapterPosition());

notifyItemRemoved(getAdapterPosition());

return true;

}

});

}

@Override

public void onClick(View view){

Toast.makeText(view.getContext(), moviesList.get(getAdapterPosition()), Toast.LENGTH_SHORT).show();

}

}

}

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.DividerItemDecoration;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;

RecyclerAdapter recyclerAdapter;

List<String> moviesList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

moviesList = new ArrayList<>();

recyclerView = findViewById(R.id.recyclerView);

recyclerAdapter = new RecyclerAdapter(moviesList);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

recyclerView.setAdapter(recyclerAdapter);

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,

DividerItemDecoration.VERTICAL);

recyclerView.addItemDecoration(dividerItemDecoration);

moviesList.add(“Captain America”);

moviesList.add(“The Fantastic Four”);

moviesList.add(“Howard the Duck”);

moviesList.add(“Blade”);

moviesList.add(“X-Men”);

moviesList.add(“Spider-Man”);

moviesList.add(“Daredevil”);

moviesList.add(“Hulk”);

moviesList.add(“The Punisher”);

moviesList.add(“Ghost Rider”);

moviesList.add(“Iron Man”);

moviesList.add(“Thor”);

moviesList.add(“The Avengers”);

moviesList.add(“Guardians of the Galaxy”);

moviesList.add(“Ant-Man”);

moviesList.add(“Deadpool”);

moviesList.add(“Doctor Strange”);

moviesList.add(“Logan”);

moviesList.add(“Black Panther”);

moviesList.add(“Venom”);

moviesList.add(“Dark Phoenix”);

moviesList.add(“The New Mutants”);

}

}

After applying the Interface callbacks

You don’t need to change the main and item row XML files, they are same as it is. We need to make some change in the Adapter class and Main activity java files, also we need to implement the interface callbacks in our main activity for item clicks inside recyclerView.

RecyclerViewClickInterface.java

public interface RecyclerViewClickInterface {

void onItemClick(int position);

void onLongItemClick(int position);

}

public class MainActivity extends AppCompatActivity implements RecyclerViewClickInterface

after that we need to implement those two methods that we are created on our interface.

@Override

public void onItemClick(int position) {

}

@Override

public void onLongItemClick(int position) {

}

private RecyclerViewClickInterface recyclerViewClickInterface;

public RecyclerAdapter(List<String> moviesList, RecyclerViewClickInterface recyclerViewClickInterface) {

this.moviesList = moviesList;

this.recyclerViewClickInterface = recyclerViewClickInterface;

}

recyclerAdapter = new RecyclerAdapter(moviesList, this);

Here, this means the interface.

RecyclerViewAdapter.java

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

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

private static final String TAG = “RecyclerAdapter”;

List<String> moviesList;

private RecyclerViewClickInterface recyclerViewClickInterface;

public RecyclerAdapter(List<String> moviesList, RecyclerViewClickInterface recyclerViewClickInterface) {

this.moviesList = moviesList;

this.recyclerViewClickInterface = recyclerViewClickInterface;

}

@NonNull

@Override

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

View view = layoutInflater.inflate(R.layout.row_item, parent, false);

ViewHolder viewHolder = new ViewHolder(view);

return viewHolder;

}

@Override

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.rowCountTextView.setText(String.valueOf(position));

holder.textView.setText(moviesList.get(position));

}

@Override

public int getItemCount() {

return moviesList.size();

}

class ViewHolder extends RecyclerView.ViewHolder {

ImageView imageView;

TextView textView, rowCountTextView;

public ViewHolder(@NonNull View itemView) {

super(itemView);

imageView = itemView.findViewById(R.id.imageView);

textView = itemView.findViewById(R.id.textView);

rowCountTextView = itemView.findViewById(R.id.rowCount);

itemView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

recyclerViewClickInterface.onItemClick(getAdapterPosition());

}

});

itemView.setOnLongClickListener(new View.OnLongClickListener() {

@Override

public boolean onLongClick(View view) {

// moviesList.remove(getAdapterPosition());

// notifyItemRemoved(getAdapterPosition());

recyclerViewClickInterface.onLongItemClick(getAdapterPosition());

return true;

}

});

}

}

}

MainActivty.java

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.DividerItemDecoration;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity implements RecyclerViewClickInterface{

RecyclerView recyclerView;

RecyclerAdapter recyclerAdapter;

List<String> moviesList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

moviesList = new ArrayList<>();

recyclerView = findViewById(R.id.recyclerView);

recyclerAdapter = new RecyclerAdapter(moviesList, this);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

recyclerView.setAdapter(recyclerAdapter);

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,

DividerItemDecoration.VERTICAL);

recyclerView.addItemDecoration(dividerItemDecoration);

moviesList.add(“Captain America”);

moviesList.add(“The Fantastic Four”);

moviesList.add(“Howard the Duck”);

moviesList.add(“Blade”);

moviesList.add(“X-Men”);

moviesList.add(“Spider-Man”);

moviesList.add(“Daredevil”);

moviesList.add(“Hulk”);

moviesList.add(“The Punisher”);

moviesList.add(“Ghost Rider”);

moviesList.add(“Iron Man”);

moviesList.add(“Thor”);

moviesList.add(“The Avengers”);

moviesList.add(“Guardians of the Galaxy”);

moviesList.add(“Ant-Man”);

moviesList.add(“Deadpool”);

moviesList.add(“Doctor Strange”);

moviesList.add(“Logan”);

moviesList.add(“Black Panther”);

moviesList.add(“Venom”);

moviesList.add(“Dark Phoenix”);

moviesList.add(“The New Mutants”);

}

@Override

public void onItemClick(int position) {

Toast.makeText(this, moviesList.get(position), Toast.LENGTH_SHORT).show();

}

@Override

public void onLongItemClick(int position) {

Toast.makeText(this, moviesList.get(position) + “ Removed”, Toast.LENGTH_SHORT).show();

moviesList.remove(position);

recyclerAdapter.notifyItemRemoved(position);

}

}

Output

The output is not different from regular on click. Here we just apply interface callbacks for the item clicks.

Hi everyone, myself Golap an Android app developer with UI/UX designer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store