How to Change a Button's Icon Programmatically?
Asked Answered
B

3

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?)
Bosomed answered 22/11, 2010 at 22:37 Comment(1)
Um, buttons don't have icons? Do you want to use an imagebutton?Disharmonious
D
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)
Dash answered 22/11, 2010 at 22:40 Comment(7)
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.Snocat
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 directoryBosomed
SOLVED! i used myButton.setCompoundDrawablesWithIntrinsicBounds(drawable.low, 0, 0, 0); thanks heaps mate! ur a life saverBosomed
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?Illjudged
@hooraygradschool: You're using the version that takes ints 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.Dash
Even looking at the docs, it doesn't quite jump out at you... or at least at me ;)Suspicious
I know the question is old, but is there a way to center the icon?Personnel
D
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);
    }
});
Devotee answered 28/11, 2014 at 5:55 Comment(0)
O
2

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

Opheliaophelie answered 6/7, 2023 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.