Card View Click on Card Move To New Activity

NexusOnly picture NexusOnly · Nov 23, 2014 · Viewed 66.2k times · Source

I am new to Android programming and was working on a card layout. I was wondering, how do I make it clickable?

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"

I have that on my card widget and then I wanted to know where to put an on clickable action? I want to be able to click the card, it gets the id of the card, and then displays a new intent activity

This is my code for the activity to load the adapter

setContentView(R.layout.activity_my);


    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    ContactAdapter ca = new ContactAdapter(createList(30));

    recList.setAdapter(ca);

Answer

Mahdi Astanei picture Mahdi Astanei · Sep 15, 2015

In Your Adapter java file and inside of "ViewHolder" you will find:

public ContactViewHolder(final View v) {
    super(v);
}

Write blow Code:

v.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        v.getContext().startActivity(new Intent(v.getContext(), YOUR_ACTIVITY_TO_START.class));
    }
});