2

I have 5 simple buttons and I want them to behave like radio buttons, but without the tick box (or whatever they call them). They are aligned in row and I want only one of them to be selected at once, and when a button is selected it must keep its selected style. My layout code looks like this:

        <Button
            android:id="@+id/button1"
            android:layout_width="150dp"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_margin="0dp"
            android:text="@string/mybutton1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/button1"
            android:text="@string/mybutton2" />

        // and so on

My code:

    mybtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mybtn1.setPressed(true);
        }
    });

So, I don't know if I can do this on the layout or in the code. Is there any way to make the button stay in selected state (and selected style) when I click on it? Also, is it possible to assign the event to all these buttons?

Thanks!

ali
  • 10,927
  • 20
  • 89
  • 138

2 Answers2

6

just set their selected state in the onClickListener:

myButton.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v)
    {
        //handle click
        v.setSelected(true);
    }
});

the clicked state is just the selected state, which you can toggle on and off for effect.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • I don't know. When I touch the button it gets a blue background color, which is the state I want. When I select it as you say above it only changes the color of the text and border to white, so it looks like it's something different. – ali Mar 04 '13 at 18:59
  • @ali, you need to create your own [selector](http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList) to get the color and style behavior you want. – Phil Mar 04 '13 at 19:00
  • O.K. I did it. How can I assign the event to all the buttons? Can I find buttons by tag or type? – ali Mar 04 '13 at 19:02
  • @ali, Your `Activity` can implement the interface `View.OnClickListener`, then for each button, add the XML attribute `android:onClick="onClick"`, or set them programmatically with `myButton.setOnClickListener(this)`. – Phil Mar 04 '13 at 19:04
0

If your desired behaviour for the buttons is to use them as checboxes you can use ToggleButton.

In your case will actually be a list of Toggle Buttons and when you click one, you will have to unselect all the others using:

for (ToggleButton btn : buttons)
    if (btn != crtButton)
        btn.setChecked(false);
niculare
  • 3,629
  • 1
  • 25
  • 39
  • This changes the text to whatever is the default textOn and textOff is. So it makes it difficult to use dynamically set text for this toggle button, if I'm not mistaken. – TeePaps Oct 06 '13 at 22:33