How to hide image of previous radio button when user selects another radio button android?

669 Views Asked by At

when user click first item, it should get checked and image should visible and when clicking second item, first item tick box should become invisible and so on. Inoroder to place image at right, i made custom drawable icon by placing at right. Code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
           android:button="@android:color/transparent"
            android:checked="true"
            android:drawableRight="@drawable/tick"
            android:text="Radio 0" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Radio 1"
            android:layout_margin="10dp"
            android:button="@null" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Radio 2"
            android:layout_margin="10dp" 
            android:drawableRight="@drawable/tick"
            android:button="@null"/>     
    </RadioGroup>
</RelativeLayout>

 public class RadioButtonEx extends Activity {
                           @Override
            protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.sortfilterclick);

                            radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
            radioButton1=(RadioButton)dialog.findViewById(R.id.radio1);
            radioButton2=(RadioButton)dialog.findViewById(R.id.radio2);
            radioButton3=(RadioButton)dialog.findViewById(R.id.radio3);
                            radioButton1.setOnClickListener(radioButtonOnClickListener);
            radioButton2.setOnClickListener(radioButtonOnClickListener);
            radioButton3.setOnClickListener(radioButtonOnClickListener);
                            private final OnClickListener radioButtonOnClickListener= new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                /* RadioButton rb = (RadioButton) v;
                 Toast.makeText(SortFilterPopupActivity.this, rb.getText(), 
                 Toast.LENGTH_SHORT).show();*/
                switch(v.getId()){
                case R.id.radio2:
                                             // this is not working
                    //Drawable d=radioButton2.setCompoundDrawables(null, null, R.drawable.tick, null);
                    Drawable tick=getResources().getDrawable(R.drawable.tick);
                    radioButton2.setCompoundDrawables(null, null, tick, null);
                    Toast.makeText(SortFilterPopupActivity.this, radioButton2.getText().toString(), Toast.LENGTH_SHORT)
                    .show();
                    break;
                }
            }


        };}

Expected output is :

![enter image description here][1]
1

There are 1 best solutions below

2
On

Since you want to hide the radio button icon alone, maybe you can use a custom style and prevent programming in excess, I have not test this but it should be something like this,

@drawable/custom_btn_radio

custom_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/radio_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/a_transparent_drawable" />

    <item android:state_checked="true" android:state_pressed="true"
          android:drawable="@drawable/a_transparent_drawable" />
    <item android:state_checked="false" android:state_pressed="true"
          android:drawable="@drawable/radio_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/a_transparent_drawable" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/radio_off_selected" />

    <item android:state_checked="false" android:drawable="@drawable/radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/a_transparent_drawable" />
</selector>

Replace the drawables with your own, basically the a_transparent_drawable should be something like this

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

  <solid
       android:color="@android:color/transparent" />

</shape>

So when you the radio button is pressed it should change its background to transparent leaving the text alone.

Hope it helps.