2

I am a newbie of android programming and trying to make a single choice button group.

For example, There are 3 buttons in my application and when one button is clicked, the others should be unclicked and uncolored.

How can I make this in effective way?

Just give a hint :( like what class or method should I use..?

  • use [RadioGroup](http://developer.android.com/reference/android/widget/RadioGroup.html) for your requirement – MilapTank Sep 05 '14 at 06:05

3 Answers3

0

Do you mean this:

Button myButton1;
Button myButton2;
Button myButton3;

myButton1 = (Button) findViewById(R.id.button1);

myButton1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        myButton1.setBackgroundColor(Color.Green); 
        myButton2.setBackgroundColor(Color.Gray); 
        myButton3.setBackgroundColor(Color.Gray); 
    }
});

myButton2 = (Button) findViewById(R.id.button1);

myButton2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        myButton2.setBackgroundColor(Color.Green); 
        myButton1.setBackgroundColor(Color.Gray); 
        myButton3.setBackgroundColor(Color.Gray); 
    }
});

myButton3 = (Button) findViewById(R.id.button1);

myButton3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        myButton3.setBackgroundColor(Color.Green); 
        myButton1.setBackgroundColor(Color.Gray); 
        myButton22.setBackgroundColor(Color.Gray); 
    }
});
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
0

Try this way,hope this will help you to solve your problem.

/drawable/radio_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" android:state_checked="true" android:state_pressed="false"/>
    <item android:drawable="@android:color/black" android:state_checked="false" android:state_pressed="false"/>
    <item android:drawable="@android:color/white" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@android:color/black" android:state_checked="false" android:state_pressed="true"/>
</selector>

/drawable/radio_text_color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/black" android:state_checked="true" />
    <item android:color="@android:color/black" android:state_pressed="true"/>
    <item android:color="@android:color/white" />
</selector>

/layout/customradiobutton.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/darker_gray"
    android:gravity="center">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radio_selector"
            android:text="Radio1"
            android:textColor="@drawable/radio_text_color_selector"
            android:checked="true"
            android:padding="10dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radio_selector"
            android:text="Radio2"
            android:layout_marginLeft="5dp"
            android:textColor="@drawable/radio_text_color_selector"
            android:padding="10dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radio_selector"
            android:text="Radio3"
            android:layout_marginLeft="5dp"
            android:textColor="@drawable/radio_text_color_selector"
            android:padding="10dp"/>
        </RadioGroup>

</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

I'm not experienced developer too. But the answer would depend of what u want to achieve. You can use as andruboy mentioned radio group, but personally I don't like how the radio buttons looks like so very often I create my own radio group using button which looks like this.

myButton1 = (ImageButton) findViewById(R.id.button1);
myBytton2 =  (ImageButton) findViewById(R.id.button2);
myBytton3 =  (ImageButton) findViewById(R.id.button3);
// this variable indicate which button was clicked as last, u can use this info for next action
int whichButtonClicked = 1;

myButton1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
     whichButtonClicked = 1;
     myButton1.setBackgroundColor(Color.Red);
     myButton2.setBackgroundColor(Color.Gray);
     myButton3.setBackgroundColor(Color.Gray);

}

});
myButton2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
     whichButtonClicked = 2;
     myButton1.setBackgroundColor(Color.Gray);
     myButton2.setBackgroundColor(Color.Red);
     myButton3.setBackgroundColor(Color.Gray);

}

});

and for third button code would look similar.

I wrote it from head, if something wouldn't work, give me a word I would test it later.

I hope this answer help u

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
MyWay
  • 1,011
  • 2
  • 14
  • 35