Home » Android » java – Calling activity from ViewHolder in RecyclerView?

java – Calling activity from ViewHolder in RecyclerView?

Posted by: admin June 15, 2020 Leave a comment


I’ve got a RecyclerView which is loading the content of my String arrays which works fine, however I want to open a new activity depending on which view they have pressed.

What I have done is created an array called classes like so:

<array name="classes">

These are stored in an array, and passed to my MainActivityList adapter below:

String[] classes = resource.getStringArray(R.array.classes);
MainActivityList adapter = new MainActivityList(titles,content, images, classes);

I’ve managed to add the OnClickListener to the ViewHolder and output what class is assigned to each view to the log, however I cannot figure out or get working, how to launch another activity.

The class name would be something like ClassOne.class for example

public class MainActivityList extends RecyclerView.Adapter<MainActivityList.ViewHolder>  {
    private String[] mTitles;
    private String[] mContent;
    private String[] mClasses;
    private TypedArray mImages;
    private Context context;

    public MainActivityList(String[] titles, String[] content, TypedArray images, String[] classes) {
        this.mTitles = titles;
        this.mContent = content;
        this.mImages = images;
        this.mClasses = classes;

    public ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i) {
        final View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main_card, viewGroup, false);
        ViewHolder vh = new ViewHolder(v);

        v.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                ViewHolder vh = (ViewHolder)v.getTag();
                Log.v("DEBUG", "Clicked" + vh.classes);
        return vh;

    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.classes = mClasses[position];

    public class ViewHolder extends RecyclerView.ViewHolder {
        public  TextView titleView;
        public  TextView contentView;
        public  ImageView imageView;
        public String classes;

        public ViewHolder(View v) {
            titleView = (TextView) v.findViewById(R.id.card_title);
            contentView = (TextView) v.findViewById(R.id.card_content);
            imageView = (ImageView)v.findViewById(R.id.card_image);

    public int getItemCount() {
        return mTitles.length;

How to&Answers:

I was also looking for a solution, and I found this post:

By using the OnItemClickListener interface, you should be able to call startActivity in your activity.

adapter.SetOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(View v , int position) {
        // This is in an Activity so should be able to start new activity, etc.


Update: I have tested, the method mentioned in the blog worked for me.


Intent intent = new Intent(viewObject.getContext(),ActivityName.class);