30

I already have the button:

<Button 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"
android:drawableLeft="@drawable/empty"
android:id="@+id/buttonMyText" 
android:text="  myText" 
android:textSize="20px" 
android:gravity="left">
</Button>

I have the "empty" icon show on the button when the program starts.

What I want to do is change the button's icon automatically from my code (low, medium and high) based on user inputs

I tried:

Button myButton = bla... bla... bla...

But I cant figure out

myButton.(what?)
ZiGi
  • 754
  • 4
  • 11
  • 20

3 Answers3

54

If you check the docs, you'll see the code equivalent for each XML attribute.

See here: http://developer.android.com/reference/android/widget/Button.html

Searching for drawableLeft shows:

android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 6
    Also, in order, the parameters are: `left`, `top`, `right`, and `bottom` Drawables, so you can pass in multiple Drawables to have drawableLefts and drawableRights, tops, bottoms, etc. Or just pass in `null` for the ones you aren't using. – Kevin Coppock Nov 22 '10 at 22:45
  • hi brotha, the icon disappeared on my Activity so it does work :) however, how to pick the image to be used? I tried myButton.setCompoundDrawables(Drawable.createFromPath("@drawable/low"),null,null,null); and also without the "@" and also with only "low" but it still disappeared, I already added all the images to my drawables directory – ZiGi Nov 22 '10 at 23:09
  • 7
    SOLVED! i used myButton.setCompoundDrawablesWithIntrinsicBounds(drawable.low, 0, 0, 0); thanks heaps mate! ur a life saver – ZiGi Nov 22 '10 at 23:15
  • 1
    i had the same question but when i try to set the things i don't need to null eclipse says `The method setCompoundDrawablesWithIntrinsicBounds(int, int, int, int) in the type TextView is not applicable for the arguments (null, int, null, null)` why does it accept null for yall but not for me? – dylan murphy Sep 02 '11 at 03:19
  • 8
    @hooraygradschool: You're using the version that takes `int`s for the resource ID (rather than ready-to-use Drawables). In that case, you need to pass in `0` to indicate that you don't want a drawable. – EboMike Sep 02 '11 at 06:32
  • 2
    Even looking at the docs, it doesn't quite jump out at you... or at least at me ;) – Peter Ajtai Nov 18 '11 at 19:17
  • 1
    I know the question is old, but is there a way to center the icon? – dequec64 Mar 22 '16 at 16:44
19

if you want to change icon at button click event then try this code...

buttonMyText.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
       buttonMyText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ImageNameHere, 0, 0, 0);
       buttonMyText.setTextColor(Color.BLACK);
    }
});
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38
0

You can use MaterialButton instead of Button. The correct declaration of material button is as:

MaterialButton button = findViewById(R.id.your_material_button_id);

By the above declaration then we have:

enter image description here

C.F.G
  • 817
  • 8
  • 16