Numair
Numair

Reputation: 1062

Custom Radio button

i'm working on custom radio button. it works fine(as it is defined in options.xml) but when i switch from options.xml to main.xml, it turns default, means it is no more highlighted. it should work like until i press it it should not turn to default.. here is radiobutton_selector.xml

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

i'm using these in options.xml to call radio button settings.

        <RadioGroup
            android:id="@+id/sound"
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:layout_gravity="right"
            android:layout_marginRight="0dp"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:padding="0dp" >

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:button="@drawable/radiobutton_selector"
                android:id="@+id/on"
                />

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:button="@drawable/radiobutton_selector"
                android:id="@+id/off"
                />
        </RadioGroup>

Please help me to figure out the issue. Thanks in advance !!!!!

Upvotes: 0

Views: 5466

Answers (1)

Herry
Herry

Reputation: 7087

You can do like this for your RadioGroup. you need to save which radio Button you have selected ,for that you can use one variable like below.

int check_radio=-1;
public static final int RADIO_BUTTON_ON=1;
public static final int RADIO_BUTTON_OFF=2;

       mRadioGroup
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {

                        switch(checkedId){

                                            case R.id.on:
                                            //Radio Button on is True
                                            check_radio=RADIO_BUTTON_ON;
                     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                     SharedPreferences.Editor editor = settings.edit();
                      editor.putInt("RadioButton", RADIO_BUTTON_ON);
                     // Commit the edits!
                     editor.commit();
                                            break;
                                           case R.id.off:
                                          //Radio Button off is True
                                           check_radio=RADIO_BUTTON_OFF;
                       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                     SharedPreferences.Editor editor = settings.edit();
                      editor.putInt("RadioButton", RADIO_BUTTON_OFF);
                     // Commit the edits!
                     editor.commit();
                                          break;
                    }
                });

Now whey your Activity 's Resume you can check one condition like below. Get Value from SharedPrefrence Like below code;

   //If you have save your value in SharedPrefrence it will return your stored int value.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   check_radio = settings.getInt("RadioButton", -1);


  if(check_radio==RADIO_BUTTON_ON){
     mRadioOn.setChecked(true);
  }else if(check_radio==RADIO_BUTTON_OFF){
    mRadioOff.setChecked(true);
  }

you can use SharedPreferences by below step

   public static final String PREFS_NAME = "MyPrefsFile";
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

Upvotes: 2

Related Questions