Reputation: 23962
In XML we can set drawableLeft using this way:
<Button
android:id="@+id/previewBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/white_btn"
android:drawableLeft="@drawable/green_circle"
android:drawablePadding="16dp"
android:text="Button" />
How to do same thing programmatically?
Upvotes: 6
Views: 9773
Reputation: 9284
Yes, use setCompoundDrawablesWithIntrinsicBounds
and define the drawable for the first parameter, then 0 for all the others.
The code should look something like this:
Button b = findViewById(R.id.myButton);
b.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);
If your drawable was created in code as well then you need to use the other setCompoundDrawablesWithIntrinsicBounds method which takes 4 drawables, and pass null for all but the left.
Upvotes: 19
Reputation: 37516
The method to use is setCompoundDrawablesWithIntrinsicBounds. This method takes all four drawable options (left, top, right, bottom), so if you want only left, pass in null
for the others.
Upvotes: 2