Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How can I do "if else condition" to the buttons?

Hello,

I have 3 buttons, if I choose to press one of them the 2 of the buttons should be inactive, but if I choose to Unclick to the button1, the 2 button is active.

How can I do it?

this is my code:

MainActivity.java: button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { button1.setSelected(!button1.isSelected()); }

    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

      button2.setSelected(!button2.isSelected());
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button3.setSelected(!button3.isSelected());
        }
    });

}

drawable.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/red_new" />

<item android:state_selected="false"
          android:drawable="@drawable/gray_new" />

thank for help :)