0

I have a CardView list containing around 20 cards made using RecyclerView and an Adapter.

After a Card item is clicked, I want it to start a new intent containing another CardView list. I can do this, but I also want it to set card colors depending on the card position clicked.

For example - If card item Red is clicked, it should start new intent class and set card colors to shades of Red (I can define it). And similarly with other color card items.

Is this possible?

Chinmay
  • 119
  • 2
  • 11

3 Answers3

0

You can simply get the position of CardView from Adapter.

This will help you.

Community
  • 1
  • 1
Mann
  • 560
  • 3
  • 13
0

First pass your color as a string to your new activity with bundle:

Intent mIntent = new Intent(mContext, YourNewActivity.class);
mIntent.putExtra("color", yourColorString);
startActivity(mIntent)

In your new Activity get color from bundle.

String color = "#000";
Bundle bundle = getIntent().getExtras();
if(bundle != null){
    color = bundle.getString(color,"#000");
}

Pass your color to your adapter in your adapter constructor and in adapter find your card view and set background color like below:

cardView.setBackgroundColor(Color.parseColor(color));

Good luck.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • Can I in any way use a String array here? – Chinmay Dec 11 '15 at 10:38
  • Yesy if you pass your string array in bundle you can get and use in your new activity. But if you pass string array i think you'll need to pass a position too to your new activity. – savepopulation Dec 11 '15 at 11:18
-1

do it in this way...inside your adapter

@Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
       //..... your rest code

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,SecondActivity.class);
                intent.putExtra("CardPosition",position); //for positiion
                intent.putExtra("CardColor",
                    list.get(position).getColor()); //for value
                context.startActivity(intent);
            }
        });
    }

in second activity you can get position of cardview...change method as per your requirement

H Raval
  • 1,903
  • 17
  • 39
  • `"CardPosition"` `"CardColor"` are these just reference names for other classes? – Chinmay Dec 11 '15 at 10:31
  • no its name of putextra argument...you can get this argument on other activity with these names...like getArguments().getExtra("CardPosition"); – H Raval Dec 11 '15 at 11:12