Card View Click on Card Move To New Activity
Asked Answered
A

6

22

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);
Anchises answered 22/11, 2014 at 23:38 Comment(2)
Maybe you can get some Ideas from here -> https://mcmap.net/q/49778/-recyclerview-onclickValle
Check this link regarding click listener on RecyclerView https://mcmap.net/q/587907/-how-to-click-recyclerview-items-in-activity-duplicateCaesura
G
11

If you used the implementation correctly, your code should go like this:

card - is the card view you instantiated to display on your ui


card.setOnClickListener(...);

In your implementation of the onClickListener, you should have this:

@Override
public void onClick(Card c ,View v) {
    Intent intent = new Intent(MyActivity.this, NextActivity.class);
    startActivity(intent);
}

that is pretty much all you need to start a new activity from the card

Godliman answered 22/11, 2014 at 23:50 Comment(2)
I don't know if I did implement it correctly. I have a recycler view that inside of it I populate it with CardView widgets. Whenever I attempt to do a recList (variable for the recycler list) and set an onClicklistener, I can't seem to get the onClick(card C, view v) i only get onClick(view v)Anchises
this answer failed to address context. see answer by Mahdi for correct answer. You must grab context through one of the views (a textview on the card for example).Lawful
R
22

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));
    }
});
Retributive answered 15/9, 2015 at 14:20 Comment(0)
G
11

If you used the implementation correctly, your code should go like this:

card - is the card view you instantiated to display on your ui


card.setOnClickListener(...);

In your implementation of the onClickListener, you should have this:

@Override
public void onClick(Card c ,View v) {
    Intent intent = new Intent(MyActivity.this, NextActivity.class);
    startActivity(intent);
}

that is pretty much all you need to start a new activity from the card

Godliman answered 22/11, 2014 at 23:50 Comment(2)
I don't know if I did implement it correctly. I have a recycler view that inside of it I populate it with CardView widgets. Whenever I attempt to do a recList (variable for the recycler list) and set an onClicklistener, I can't seem to get the onClick(card C, view v) i only get onClick(view v)Anchises
this answer failed to address context. see answer by Mahdi for correct answer. You must grab context through one of the views (a textview on the card for example).Lawful
T
5

Addind onClick to the cardView did it for me:

     <android.support.v7.widget.CardView
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:id="@+id/bankcardId"
            android:layout_width="160dp"
            android:layout_height="190dp"
            android:layout_margin="10dp"
            android:onClick="P1_bay">

Then call it in your java function as below:

    public void P1_bay(View view) {
    Toast.makeText(this, "You have clicked P1", Toast.LENGTH_LONG).show();
}
Thiouracil answered 12/10, 2018 at 18:1 Comment(0)
K
3

You can use viewHolder class as follow

public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);

       itemLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
             //  perfoem your action here
            }
        });
    }
Knute answered 4/11, 2015 at 14:41 Comment(0)
P
1

import android.view.View;

Intent intent = new Intent(view.getContext(),YourActivity.class); view.getContext().startActivity(intent);

Philter answered 2/1, 2017 at 22:1 Comment(0)
E
1

You could implement the View.OnClickListener() interface to your class and then in your onCreate() method you could write findViewById(R.id.cardview).setOnClickListener(this). You could then Override the onClick() method and do what you want to do when the user clicks the card.

It would look like this:

public class MainActivity extends Activity implements View.OnClickListener()
{
     public void onCreate(Bundle savedInstanceState)
     {
       super.onCreate(savedInstanceState);
       // load the layout
       setContentView(R.layout.filters);
       // get the id of the CardView and attach an onClickListener to it
       findViewById(R.id.cardList).setOnClickListener(this)
     }
     @Override
     private void onClick(View view)
     {
        if(view.getId == R.id.cardList)
        {
         //Do something Like starting an activity
         Intent intent = new Intent(MyActivity.this, NextActivity.class);
         startActivity(intent);
        }
     }
}
Epithet answered 12/7, 2018 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.