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

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.


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

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

        }
    });

Tags:

Android

Layout