15

I know this is a question that has been asked many times before, but I can't seem to solve it in my code. I have two buttons, and when one is pressed, I would like to keep it in its selected state, and vice versa. I have tried doing it using but the setSelected and setPressed, but I can't seem to get it to work. Here is the code I am using:

    region_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            objects = category;
            adap.notifyDataSetChanged();
            proximity_button.setPressed(false);
            region_button.setPressed(true);

        }
    });

    proximity_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            objects = proximity;
            adap.notifyDataSetChanged();
            region_button.setPressed(false);
            proximity_button.setPressed(true);

        }
    });

Edit: Based on the comments, I need to add that I have a custom xml background for the buttons, and would like to retain the current look.

coder
  • 10,460
  • 17
  • 72
  • 125
  • 1
    Why don't you just use RadioGroup and RadioButton, then set right attributes to your RadioButtons, using a selector it should do the trick ! – Cehm Feb 16 '12 at 20:14

5 Answers5

21

I searched on Google and found this Stack Overflow post:
How can i keep one button as pressed after click on it?

mycodes_Button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mycodes_Button.setPressed(true);
        return true;
    }
});

But read the comment, it's pretty interesting!

Pang
  • 9,564
  • 146
  • 81
  • 122
Cehm
  • 1,502
  • 1
  • 12
  • 14
  • Almost right-just for other's reference, the other button state also needs to be set to false, but I'll accept anyhow because it got me on the right track. Thanks! – coder Feb 16 '12 at 20:36
  • 8
    using setSelected (http://stackoverflow.com/a/3417158/667834) worked better for me so I could still have functionality on release – richy Oct 18 '12 at 05:28
4

Why are you using Buttons? CheckBox or RadioGroup is best solution for these case

checkBox.setChecked(false);
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
3

I know I'm late to the party but I tried doing the above and it was still acting like a temporary state (unless you want to keep your finger on it).

What worked for me is to have state_selected="true" in the XML file + imageButton.setSelected(true) in my java file. see below.

ibType1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
    ibType1.setSelected(true);
    ibType2.setSelected(false);
}//onClick
});

XML:

<item android:state_selected="true">
<shape android:shape="rectangle">
    <corners android:radius="45dp" />
    <solid android:color="@color/colorAccent" />
    <stroke android:width="1dp" android:color="#000000"/>
</shape>

This will allow you to have image buttons or button with a specific background state selected programmatically up until you decide to selected another image button or button.

mehdi
  • 340
  • 4
  • 17
Ants
  • 390
  • 1
  • 3
  • 18
1

Here's a thought, disable the pressed button and enable the others. Have the disabled button layout similar to the pressed layout. The user will see it as pressed, but it's actually disabled.

black
  • 781
  • 1
  • 7
  • 22
0

A checkbox could work, but if you're after a UI closer UISegmentedControl on iOS, which is possible wrapping all the buttons needed in a LinearLayout, then you could do something along the lines of:

public void onClick( View v ){
    if ( v.getID == R.id.btn1 ) {
        btn1.setEnabled(false);
        btn2.setEnabled(true);
        // do stuff
    }
    else if ( v.getId() == R.id.btn2 ) {
        btn2.setEnabled(false);
        btn1.setEnabled(true);
        // do stuff
    }
}

Of course, in your drawable folder, you would add background colour, borders, and other visual stuff for each state.

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • I am looking for a look similar to NSSegmentedControl on iOS, but the problem with your solution, is once a button is not enabled, clicking on it again will do nothing. – coder Feb 16 '12 at 20:30
  • @coder That's the idea behind UIsegmentedControl (my bad earlier for the typo). To re-enable a disabled button, another one needs to be pressed. It functions exactly like a radio group. – Mike D Feb 16 '12 at 20:39