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);
import android.view.View;
Intent intent = new Intent(view.getContext(),YourActivity.class); view.getContext().startActivity(intent);
You could implement the
View.OnClickListener()
interface to your class and then in youronCreate()
method you could writefindViewById(R.id.cardview).setOnClickListener(this)
. You could then Override theonClick()
method and do what you want to do when the user clicks the card.It would look like this:
Addind onClick to the cardView did it for me:
Then call it in your java function as below:
In Your Adapter java file and inside of "ViewHolder" you will find:
Write blow Code:
If you used the implementation correctly, your code should go like this:
In your implementation of the onClickListener, you should have this:
that is pretty much all you need to start a new activity from the card
You can use viewHolder class as follow