Calling setCompoundDrawables() doesn't display the Compound Drawable

AndroidAndroid LayoutAndroid Drawable

Android Problem Overview


After I call the setCompoundDrawables method, the compound Drawable is not shown..

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);

Any thoughts?

Android Solutions


Solution 1 - Android

Solution 2 - Android

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 );

Solution 3 - Android

Image is blank because it hasn't got specified bounds. You may use setCompoundDrawables() but before you should specify image's bounds, using Drawable.setBounds() method

Solution 4 - Android

Example set to the top:

view.setCompoundDrawablesWithIntrinsicBounds(
    null,
    getResources().getDrawable(R.drawable.some_img),
    null,
    null
);

arguments order: (left, top, right, bottom)

Solution 5 - Android

A little bit simpler again:

Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );

Solution 6 - Android

The Image is not shown as you didn't specify the bounds, so you have 2 options here.

1st Method

Use setCompoundDrawablesWithIntrinsicBounds method, as shown below

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn. setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null);

2nd Method

You can apply bounds to the drawable before applying to the TextView, as shown below

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
myDrawable.setBounds( 0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
btn.setCompoundDrawables(myDrawable, null, null, null);

That's it.

Solution 7 - Android

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);

Solution 8 - Android

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)

Solution 9 - Android

Solution 10 - Android

Example with Kotlin:

    val myView = layoutInflater.inflate(R.layout.my_view, null) as TextView
    myView.setCompoundDrawablesWithIntrinsicBounds(0, myDrawable, 0, 0)

Solution 11 - Android

In Kotlin with Databinding

binding.tvTime.setCompoundDrawablesWithIntrinsicBounds(
            ResourcesCompat.getDrawable(resources, android.R.drawable.ic_menu_recent_history, null),
            null,null,null)

where tvTime is textView and Position (Rigth,Top,Left,Bottom), More about binding is here https://stackoverflow.com/a/72360048/12272687

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhunterpView Question on Stackoverflow
Solution 1 - AndroidhunterpView Answer on Stackoverflow
Solution 2 - Androidaryan bahmaniView Answer on Stackoverflow
Solution 3 - AndroidteoREtikView Answer on Stackoverflow
Solution 4 - AndroidAndrewView Answer on Stackoverflow
Solution 5 - AndroidAlecsView Answer on Stackoverflow
Solution 6 - AndroidAsad Ali ChoudhryView Answer on Stackoverflow
Solution 7 - Android許維德View Answer on Stackoverflow
Solution 8 - AndroidCoolMindView Answer on Stackoverflow
Solution 9 - AndroidBharat DodejaView Answer on Stackoverflow
Solution 10 - Androidmike.tihonchikView Answer on Stackoverflow
Solution 11 - AndroidMoriView Answer on Stackoverflow