Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Setting Background Image for RelativeLayout?

I have downloaded an image at runtime. Now I want to set it as a background for RelativeLayout. Does it possible?

like image 542
Vivek Avatar asked Feb 05 '11 10:02

Vivek


People also ask

How can you set background color of layout used in above application?

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.

Is RelativeLayout deprecated?

relativelayout is deprecated now.


2 Answers

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);
like image 186
SteD Avatar answered Nov 15 '22 21:11

SteD


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);
like image 24
Melvin Miller Avatar answered Nov 15 '22 20:11

Melvin Miller