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


Android Problem Overview

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

Android Solutions

Solution 1 - Android


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.

Solution 2 - Android

Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);

Solution 3 - Android

final Drawable drawableTop = getResources().getDrawable(R.drawable.btn_check_buttonless_on);

btnByCust.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

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

Solution 4 - Android

    	Button button = (Button) findViewById(;
        button.setCompoundDrawables(left, top, right, bottom);

Solution 5 - Android

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

Solution 6 - Android


Solution 7 - Android

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

Solution 8 - Android

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


fun TextView.setTopDrawable(icon: Int) {


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


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
QuestionManeeshView Question on Stackoverflow
Solution 1 - AndroidTanmay MandalView Answer on Stackoverflow
Solution 2 - AndroidKirit VaghelaView Answer on Stackoverflow
Solution 3 - AndroidGilView Answer on Stackoverflow
Solution 4 - Androidiamtheexp01View Answer on Stackoverflow
Solution 5 - AndroidieselisraView Answer on Stackoverflow
Solution 6 - AndroidNikita Yo LAHOLAView Answer on Stackoverflow
Solution 7 - AndroidAlbert GaoView Answer on Stackoverflow
Solution 8 - AndroidAklesh SinghView Answer on Stackoverflow