Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the image of a button with every click?

I created a button in the layout . In the Drawable folder I created a XML file named btn01_state. The btn01_state.xml is assigned to the button i created through "android:background=@drawable/btn01_state"

Now, the button has a default image img1.when i click on the button, the image1 changes to img2, and once i release the clicked mouse button, the image2 again changed to img1 again.

what i want to do is,to change the image of the button with evey click.

for an example, initially btn01 has img01

if btn01 is pressed==> set img of btn01 to img02 and keep img02 till the btn01 is pressed again. Now, btn01 has img02 on it.

When btn01 is pressed, set img01 to btn01.

I hope this clarified more what i want to do.

btn_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android_blue"
      android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
      android:state_focused="true" />
<item android:drawable="@drawable/ic_launcher" />

main.xml

<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/btn01"
    android:background="@drawable/btn01_state"/>
like image 621
Androelpha Avatar asked May 06 '12 11:05

Androelpha


People also ask

How do I change the image on a button?

The default button in HTML can be changed to an image using CSS. The required button is selected using the respective CSS selector. The background property can then be set to include a background image and change the image type as required. The border of the button can also be removed to show only the image itself.

How do I change the click image in HTML?

Create <img> element in the HTML code. Add style to <img> element and set display properties to none. Create a JavaScript “show()” function that can access the image and change the display property to block. Add button in HTML code which calls “show()” function when user clicks on it.

Can you put an image in a button?

Button with an ImageYou can include an <img> element inside your <button> element to display the image on the button. You can still add text to the button, resulting in a combination of image and text.

How do I change what button I click on?

We use the textContent property to change the button's text every time it's clicked. The textContent property represents the text content of the node and its descendants. If you need to change the button's inner HTML, use the innerHTML property instead of textContent .


2 Answers

You can do it easily within the code.

boolean isPressed = false;
button.setOnClickListener(buttonListener);

OnClickListener buttonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(isPressed)
           button.setBackgroundResource(R.drawable.icon1);
        else
           button.setBackgroundResource(R.drawable.icon2);

        isPressed = !isPressed;
   }
};
like image 128
Murat Nafiz Avatar answered Nov 12 '22 18:11

Murat Nafiz


Simple way

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.locationbutton_on));
                }
        }); 
like image 22
Parag Chauhan Avatar answered Nov 12 '22 17:11

Parag Chauhan