0

enter image description hereI'm working in a project called gift card, which is a simple app. When a user is in an activity, they can click in a photo and when they click, it will appear as background of another activity. I'm trying to solve this but nothing is working. Can you help me but please in a simple way because im a beginner in Android Studio. Thank You !!!

3 Answers3

0

You can simply set a drawable on an ImageView as in the below code:

imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_drawable));
Rohan Sharma
  • 69
  • 3
  • 13
0

getDrawable(id) is deprecated since API 22. Now you need to specify Theme.

 getResources().getDrawable(id, context.getTheme());

In order to support previous android versions use the following:

ContextCompat.getDrawable(context, R.drawable.***)

Android getResources().getDrawable() deprecated API 22

Community
  • 1
  • 1
Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • Use "this" instead of "context" – Access Denied Nov 29 '15 at 12:13
  • but in fact maybe have no sence because i can do all this codin in activity 2 you understand what im saying because in activity 2 the client can click in the photo and when they click ,the background with change of activity one the data can i sent with intent but image i dont know how to do that because im and new in android – Fation Joni Nov 29 '15 at 12:20
0

Try This

imageView.setImageResource(getResources().getIdentifier(
            "imagename", "drawable", context.getPackageName()));

Make sure you are putting right Image Name and Package name in parameters.

As getResources().getDrawable() is being depreciated in API 22 , You can also try this

ContextCompat.getDrawable(context, R.drawable.***)

You can implement the version check by using following code.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}

You should use the getDrawable(int, Theme) method for API 21 instead of getDrawable(int).Hence Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null). hope it will work for you .

Salman Nazir
  • 2,759
  • 2
  • 28
  • 42
  • i have activiti one and activity 2 when in activity 2 are some photo but will a small dimension when somebody click in a photo i want to change the background of the activity one this is what i want i have sent data with intent but sent image im 5 days working with this and i dont know do – Fation Joni Nov 29 '15 at 11:53
  • If you want to set background of 1st activity from the image selection in 2nd activity , then you need to put the ID of that particular image In Intent Using `Intent i = new Intent(SecondScreen.this, FirstScreen.class); i.putExtra("IMAGE_U_NEED", "IMAGE_ID"); startActivityForResult(i);` On First Activity you can get that Image by his ID through onActivityResult `protected void onActivityResult(int requestCode, int resultCode, Intent data) ` This is in your First Activity , you pick Image id from data and set it to background – Salman Nazir Nov 30 '15 at 08:15
  • thank you so much but i have done ,i have solve the problem – Fation Joni Dec 01 '15 at 07:39
  • Good , If my answer seems helpful for you , you can vote. – Salman Nazir Dec 01 '15 at 07:42