1

I am trying to start a new Activity based on the user selection of an item in Recyclerview. The user would tap on a selection and I want to get the background of that selected item and set it to the activity. So far I have tried

  1. Intent
  2. setContentView(View) as well as setContentView(Resource)
  3. View switcher.

None of these worked for me. I also would like to let the user hit back button and go back to the list of selection, should they change their mind. What is the best way to do this?

EDIT: the background is a drawable not a color. Intent was obviously not an option because I can't pass the drawable.

UPDATE: I set a public Drawable object in my main class and assigned it the background image of my selected item by calling view.Background(); Then I retrive that value and assign to my main layout of the next activity but it is still not working.

LinearLayout lt = (LinearLayout) findViewById(R.id.firstLayout);
lt.setBackground(MainClass.backgroundDrawable);

This part is not working in my new activity. I see the value is correct but that same Drawable is not assigned to the activity.

The_Martian
  • 3,684
  • 5
  • 33
  • 61

2 Answers2

1

You can tag each your Drawable in XML or prgramatically and then retrieve it using getTag() method. Use getResources().getIdentifier(tag name, "drawable", .getPackageName()) to get the drawable's resource id.

Now pass it as an extra in the intent

intent.putExtra("tag name",resource id);

Retrieve the drawable using

getResources().getDrawable(intent.getIntExtra("tag name", -1));

Edit

I don't really understand what you are trying to accomplish, but suppose you have an image called img.png that you want to set as the background, you could do something like this:

LinearLayout lt = (LinearLayout) findViewById(R.id.firstLayout);
lt.setBackground(R.drawable.img);
lt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick() {
        Intent intent = new Intent(getApplicationContext(), NextActivity.class);
        intent.putExtra("bg", "img");
        startActivity(intent);
    }
} 

And in your NextActivity, you can extract the bg extra and write a switch to check which drawable to put as background, assuming you are using more than one image for the background.

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
  • All my drawables are .png images stored in /drawables folder. How do I tag them? – The_Martian Sep 19 '16 at 07:14
  • @The_Martian if you know the file name of the background in your `RecyclerView`, then can't you pass that name to your next activity and in that use the drawable normally? – Amit Tiwari Sep 19 '16 at 07:17
  • @The_Martian could you post the code where you set your drawable as background in the recycler view as well as in the next activity? – Amit Tiwari Sep 19 '16 at 07:19
  • For this approach to work, you have to set your background programmatically but not in the xml. – The_Martian Sep 19 '16 at 19:06
0

I solved it after making some research and tweaked my code as follows. I declared a public Drawable in my main activity. And I tracked the position of every card view in

OnBindViewHolder(MyViewHolder vh, int position)

using

if(position == 0){
holder.view.setBackgroundResource(R.drawable.image1);
} 

Then I register onClickListener on the view in MyViewHolder constructor in which I assign the view's background to my global Drawable

gloabalDrawable = clickedView.getBackground();

I simply start the new activity and retrieve the value of the global Drawable and assign it to my layout in the new activity.

The_Martian
  • 3,684
  • 5
  • 33
  • 61