Home » Android » android – How to use relativelayout.setBackgroundDrawable() with a bitmap?

android – How to use relativelayout.setBackgroundDrawable() with a bitmap?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a RelativeLayout object and want to dynamically change the background image with a dynamically created Bitmap object (it changes its color dynamically).

I saw that when I wanted to update the background image of the RelativeLayout object that I can only choose setBackgroundDrawable() which requires a Drawable object as a parameter.

My question is, how can I convert the dynamically created Bitmap object into a Drawable object?

How to&Answers:

BitmapDrawable(obj) convert Bitmap object into drawable object.

Try:

RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);

I hope this will help you.

Answer:

You can do it by this way

Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);

Answer:

Try this,

Drawable drawable = new BitmapDrawable(bitmap);

Answer:

RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable dr = new BitmapDrawable(myImage);
rl.setBackgroundDrawable(dr);

Answer:

Drawable d = new BitmapDrawable(bitmap);

Answer:

//Create Reference for RelativeLayout

RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl);

//set Background to layout reference using the following method

Drawable drawable = getResources().getDrawable(R.drawable.bg);
layout.setBackground(drawable);

Note: R.id.rl is id of RelativeLayout

R.drawable.bg id the Image in drawable folder