28

How to add image(left) and text on button?


For illustration:

enter image description here

Roy Lee
  • 10,084
  • 13
  • 57
  • 83

3 Answers3

56

use android:drawableLeft="@drawable/image" in your layout xml

you can also do this from code

Drawable icon= getContext().getResources().getDrawable( R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
Community
  • 1
  • 1
Michael Shrestha
  • 2,517
  • 18
  • 30
  • Very nice. Is it possible to have drawable background color change? Because my Icon is white color with transparent background. – Roy Lee Jun 23 '13 at 07:08
  • 1
    it might help you http://stackoverflow.com/questions/4692642/android-customized-button-changing-text-color – Michael Shrestha Jun 23 '13 at 07:11
  • 1
    Not to worry about that, I'm still trying your answer. Is it possible to set drawableLeft with selector? – Roy Lee Jun 23 '13 at 07:25
  • Yes I've got it :) Friend, thanks for the input: drawableLeft, and Selector guide! :) It works like charm. – Roy Lee Jun 23 '13 at 08:08
  • "android:drawableLeft.." the d in drawable must be lowercase – Lele Aug 20 '16 at 13:32
9
        android:text="Documentos"
        android:drawableLeft= "@drawable/ic_document"
Everson Rafael
  • 2,013
  • 2
  • 19
  • 19
8

We can also manage the size of drawable while doing programmatically

Drawable dr = getResources().getDrawable(R.drawable.mag_green);
dr.setBounds(0, 0, 43, 40); //Left,Top,Right,Bottom
search_bar.setCompoundDrawables(dr, null , null , null);
Mahesh Babariya
  • 4,510
  • 6
  • 38
  • 53