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;


I was also looking for a solution, and I found this post: http://venomvendor.blogspot.sg/2014/07/setting-onitemclicklistener-for-recycler-view.html

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.

  • The linked article is excellent. Lots of SO answers can handle the click events, as long as the function is self-contained (i.e. writing to the Log file). This is the only solution I've seen which allows for the original calling activity/fragment to register a listener and take over the event handling. Not entirely sure if it's absolutely optimal, but it works. – ExactaBox Nov 30 '14 at 5:45
Intent intent = new Intent(viewObject.getContext(),ActivityName.class);
  • 3
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – HiDeo Jul 16 '16 at 7:58
  • nope this is just what i needed – jsnid00 Jul 29 at 20:56

Your Answer

