I have downloaded an image at runtime. Now I want to set it as a background for RelativeLayout
. Does it possible?
Important Note: We can set color or image in the background of RelativeLayout in XML using background attribute or programmatically means in java class using setBackgroundColor() for color and setBackground() method for setting image.
relativelayout is deprecated now.
Check out the setBackgroundDrawable, or maybe createFromPath in the Drawable class.
RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
rLayout.setBackground(drawable);
Instead use:
View lay = (View) findViewById(R.id.rLayout);
lay.setBackgroundResource(R.drawable.newImage);
This works because R.drawable.newImage
refers to an integer. So you could do:
int pic = R.drawable.newImage;
lay.setBackgroundResource(pic);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With