title: How to add OnClickListner to CardView?
body: <p>I am working with <code>RecyclerView</code> and <code>CardView</code>. I want to attach <code>OnClickListner</code> to each card. I tried with many answers available on stackoverflow, but they are not working for me. So far I have tried -</p>
  
  <pre><code>public class SubjectAdapter extends RecyclerView.Adapter&lt;SubjectAdapter.ViewHolder&gt; implements View.OnClickListener,
      View.OnLongClickListener{
  
  private static final String LOGCAT = "SubjectAdapter";
  private final Context mContext;
  
  List&lt;Subject&gt; SubjectsList;
  
  public SubjectAdapter(Context context) {
      super();
      this.mContext = context;
      SQLiteDatabase.loadLibs(mContext);
      DBHelper myDbHelper = new DBHelper(mContext);
      SubjectsList = new ArrayList&lt;Subject&gt;();
      SubjectsList = myDbHelper.getAllSubjects();
      myDbHelper.close();
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      View v = LayoutInflater.from(viewGroup.getContext())
              .inflate(R.layout.subject_cardview_row, viewGroup, false);
      ViewHolder viewHolder = new ViewHolder(v);
  
      // Below two lines are NOT working
      viewHolder.tvSubjectName.setOnClickListener(this);
      //viewHolder.setOnClickListener(this);
  
      return viewHolder;
  }
  
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int i) {
      Subject subject = SubjectsList.get(i);
      viewHolder.tvSubjectName.setText(subject.getSubject_Name());
      viewHolder.tvCounts.setText(String.valueOf(subject.getSubject_Number_of_Questions()));
  
      // Below two lines are NOT working
      viewHolder.tvSubjectName.setOnClickListener(this);
      //viewHolder.setOnClickListener(this);
  }
  
  @Override
  public int getItemCount() {
      return SubjectsList.size();
  }
  
  @Override
  public void onClick(View v) {
      // It's not working either
      ViewHolder holder = (ViewHolder) v.getTag();
      int position = holder.getPosition();
  
      if (v.getId() == holder.tvSubjectName.getId()){
          Log.d(LOGCAT, "tvSubjectName onClick at" + position);
          //Toast.makeText(mContext, "tvSubjectName onClick at" + position, Toast.LENGTH_LONG).show();
      } else {
          Log.d(LOGCAT, "RecyclerView Item onClick at " + position);
          //Toast.makeText(mContext, "RecyclerView Item onClick at " + position, Toast.LENGTH_LONG).show();
      }
  }
  
  @Override
  public boolean onLongClick(View v) {
      return false;
  }
  
  class ViewHolder extends RecyclerView.ViewHolder {
  
      public TextView tvSubjectName;
      public TextView tvCounts;
  
      public ViewHolder(View itemView) {
          super(itemView);
          tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
          tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
      }
  }
  }
  </code></pre>
  
  <p>As one can see, I have tried <code>setOnClickListener</code> with both <code>onCreateViewHolder</code> and <code>onBindViewHolder</code>, also as separate <code>onClick</code>, but none of them seems to be working for me. So, I want to know, <strong>How to add OnClickListner to CardView?</strong></p>