Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Card View Click on Card Move To New Activity

Tags:

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); 
like image 761
NexusOnly Avatar asked Nov 22 '14 23:11

NexusOnly


People also ask

How do I create a custom card view?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

Why is card view used in Android?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.


1 Answers

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));     } }); 
like image 177
Mahdi Astanei Avatar answered Oct 01 '22 15:10

Mahdi Astanei