Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change a Button's Icon Programmatically?

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?)
like image 973
ZiGi Avatar asked Nov 22 '10 22:11

ZiGi


2 Answers

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)
like image 141
EboMike Avatar answered Nov 13 '22 15:11

EboMike


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);
    }
});
like image 38
Ravi Makvana Avatar answered Nov 13 '22 15:11

Ravi Makvana