Cataroux
Cataroux

Reputation: 147

stateListDrawable help

So i made these two buttons.

I want to be able to click the button i made and have the button change to the whited-out image of the original button instead of on and off states you would have in a toggle button.

so far all I've figured out is something to do with stateListDrawable xml, which i placed in the 3 drawable folders. Thats the only place I've found it belongs, however, i don't know where to reference the .xml file to the main. Do you do this programmatically? If so point me in the right direction of a good tutorial. The android dev page is foreign to me. And i would have posted the buttons so you would have a better idea, but i can't until i get some more points. What a gimmick!

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
       android:drawable="@drawable/toggle_selection_pressed" />  
 <item android:drawable="@drawable/toggle_selection" />
</selector>

Upvotes: 2

Views: 793

Answers (2)

Carnivoris
Carnivoris

Reputation: 803

You'll need a variable to keep the state of the button. Put this in the main class declaration

    int isClicked = 0;

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        if (isClicked == 0){
           Demo_button.setsetImageResource(R.drawable.secondimage);
           isClicked = 1;
        }
        else{
           Demo_button.setsetImageResource(R.drawable.firstimage);
           isClicked = 0;
        }
    }
    }

Upvotes: 0

RAnderson
RAnderson

Reputation: 190

You reference your drawable xml files wherever the image for your button should be.

For instance, in the layout where you place the button it would be:

<Button
<!-- Other stuff -->
android:background="@drawable/xmlfile"
>

Upvotes: 1

Related Questions