[java] Set drawable size programmatically

It's been a while since the question was asked
but is still unclear for many how to do this simple thing.

It's pretty simple in that case when you use a Drawable as a compound drawable on a TextView (Button).

So 2 things you have to do:

1.Set bounds:

drawable.setBounds(left, top, right, bottom)

2.Set the drawable appropriately (without using of intrinsic bounds):

button.setCompoundDrawablesRelative(drawable, null, null, null)
  • No need to use Bitmaps
  • No workarounds such as ScaleDrawable ColorDrawable or LayerDrawable (what are definitely created for other purposes)
  • No need in custom drawables!
  • No workarounds with the post
  • It's a native and simple solution, just how Android expects you to do.