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);
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.
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.
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)); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With