PixelsTech
PixelsTech

Reputation: 3347

How to get drawable from a button?

Since on button, we can use android:drawableLeft to set the drawable on the left of a button. Is there way to get this drawable programmatically?

<Button android:id="@+id/detail_refresh_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/refresh" 
        android:gravity="center"
        android:drawableLeft="@drawable/progress"
/>

Is there any way to get the drawable "progress" in Java code?

Upvotes: 10

Views: 9870

Answers (4)

Hrishikesh Kadam
Hrishikesh Kadam

Reputation: 37342

Doc Link

Button.getCompoundDrawables()[index]

where index can be 0, 1, 2, 3 for drawableLeft, drawableTop, drawableRight, drawableBottom respectively.

Upvotes: 0

Rindt Lin
Rindt Lin

Reputation: 44

If the view is button,

button.getBackground().setLevel(x);
imageView.getDrawable().setLevel(x);

Upvotes: 0

JeffS
JeffS

Reputation: 2737

I think you want Button.getCompoundDrawables()[0]

Upvotes: 24

Broak
Broak

Reputation: 4187

Looking at : http://developer.android.com/reference/android/widget/Button.html

You can see that

myButton.setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)

Should give you what your looking for.

Order: Left, top, right, and bottom

Upvotes: 3

Related Questions