After I call the setCompoundDrawables method, the compound Drawable is not shown..
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setC
Use This (I tested). It works good
Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
int h = image.getIntrinsicHeight();
int w = image.getIntrinsicWidth();
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( image, null, null, null );
I needed to be using setCompoundDrawablesWithIntrinsicBounds.
It is deprecated in API 22.
This code is useful for me:
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable, null, null, null);
In Kotlin:
1) Set drawable
:
val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
or
val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
setBounds(0, 0, minimumWidth, minimumHeight)
}
2) Set TextView
:
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
or
button.setCompoundDrawables(null, drawable, null, null)