67

How to set property "android:drawableTop" of a button at runtime

Jonas
  • 121,568
  • 97
  • 310
  • 388
Maneesh
  • 6,098
  • 5
  • 36
  • 55

8 Answers8

144

Use

button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

If you use

button.setCompoundDrawables(left, top, right, bottom);

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • The method is very much correct but I want to set Resource id in place of drawable. Is there any way to do the same. – Maneesh Feb 07 '11 at 09:46
  • 3
    Yes.Use Resources resources= getResources(); Drawable drawable= resources.getDrawable(id); – Tanmay Mandal Feb 07 '11 at 09:52
  • Tanmay, I'm trying to do this too and I'm still not sure how to set drawableTop using the code. I see how to get a drawable, but how to set it on the button? – Otto Mar 01 '11 at 19:36
  • Is there a reason why this would not work in the ExpandableListView Group name ? This was the trick I wanted to use to put the group indicator on the right instead of the left. – OlivierM Oct 22 '12 at 16:38
  • 9
    Thanks just to clarify I used. b.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.drawable.x,0,0); – Ryan Heitner Jan 08 '13 at 14:23
  • getDrawable() is now deprecated – blackwolf Jul 23 '15 at 10:48
56
Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
23
final Drawable drawableTop = getResources().getDrawable(R.drawable.btn_check_buttonless_on);

btnByCust.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


 btnByCust.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);

        }
    });
Gil
  • 568
  • 5
  • 9
4
        Button button = (Button) findViewById(R.id.button);
        button.setCompoundDrawables(left, top, right, bottom);
iamtheexp01
  • 3,446
  • 9
  • 35
  • 35
  • The method is very much correct but I want to set Resource id in place of drawable. Is there any way to do the same. – Maneesh Feb 07 '11 at 09:44
  • @Maneesh Check [documentation](https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesRelativeWithIntrinsicBounds(int,%20int,%20int,%20int)) for the method – Bonatti Oct 02 '18 at 12:47
2

I use this code for use the "Theme.Holo" button with a "Custom image" at left and change it (the image)with a function that is called from various ways.

protected void app_dibujarLogojuego() {
    if(bitmaplogojuego!=null){
        bitmaplogojuego.recycle();
        bitmaplogojuego=null;
    }
    Drawable LOGO = null;
    if(verjuego.equals("COSA1")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA1);  }
    if(verjuego.equals("COSA2")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA2);  }
    if(verjuego.equals("COSA3")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA3);  }
    if(verjuego.equals("COSA4")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA4);  }

    BUTTON_DECLARED_ID.setCompoundDrawablesWithIntrinsicBounds(LOGO, null , null, null);
}
ieselisra
  • 387
  • 4
  • 16
0
 btn.setBackgroundResource(R.drawable.your_image_name_here);
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

If you are using Kotlin, you can use extension method to make things look elegant.

fun TextView.setDrawableTop(iconId: Int) {
    val icon = this.context?.resources?.getDrawable(iconId)
    this.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
}

Then you can use it like this:

// myTextView: TextView
myTextView.setDrawableTop(R.drawable.ic_happy)
Albert Gao
  • 3,653
  • 6
  • 40
  • 69
0

Create an extension function like this and set top drawable like this

tvAccepted.setTopDrawable(R.drawable.ic_preparing_order_active)

fun TextView.setTopDrawable(icon: Int) {
    this.setCompoundDrawablesRelativeWithIntrinsicBounds(0,icon,0,0)
}

where

setCompoundDrawablesRelativeWithIntrinsicBounds(left/start, top, right/end, bottom)

HalR
  • 11,411
  • 5
  • 48
  • 80
Aklesh Singh
  • 917
  • 10
  • 12