Home » Android » android – How to set an empty drawable

android – How to set an empty drawable

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m dealing with bugs in the Facebook SDK and so to remove the bugs I’ll need to set a drawable to nothing not NULL just nothing or empty space or something. How can I do that programmatically?

Drawable=...
How to&Answers:

You can use a transparent ColorDrawable:

Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);

That’s a Drawable that represents the transparent color and doesn’t have an intrinsic size.

Answer:

Empty Drawable with width and height can be made using a transparent ColorDrawable and the function setBounds(Rect)

 int left=0,top=0,right=32,bottom=32;
 Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
 transparentDrawable.setBounds(left,top,right,bottom);

If you want an empty drawable of dimensions same as another drawable(say d):

 Drawable d = getResources().getDrawable(R.drawable.my_drawable);
 Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
 transparentDrawable.setBounds(new Rect(0, 0, d.getMinimumWidth(), d.getMinimumHeight()));