-2

How to get value from selected RadioButton when I click OK button? I want to work RadioButton function when I click OK button. NOT isChecked() function. This is my Custom RadioButton Dialog Code.

txt_language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom_language_dialog);
                tha = (RadioButton) dialog.findViewById(R.id.tha);
                en = (RadioButton) dialog.findViewById(R.id.en);
                btnOK = (Button) dialog.findViewById(R.id.btn_ok);
                if (tha.isChecked()) {
                    StoreUtil.getInstance().saveTo("languages", "tha");
                    closeAllActivities();
                }
                if (en.isChecked()) {
                    StoreUtil.getInstance().saveTo("languages", "en");
                    closeAllActivities();
                }
               // btnOK.setOnClickListener(this);
                dialog.show();
            }
        });
Pyae Phyo
  • 127
  • 1
  • 1
  • 9

1 Answers1

0

Method 1 : Selected Data display in Toast.

Tested and working. Check this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
 
public class MyAndroidAppActivity extends Activity {
 
  private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btnDisplay;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    addListenerOnButton();
 
  }
 
  public void addListenerOnButton() {
 
    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);
 
    btnDisplay.setOnClickListener(new OnClickListener() {
 
        @Override
        public void onClick(View v) {
 
                // get selected radio button from radioGroup
            int selectedId = radioSexGroup.getCheckedRadioButtonId();
 
            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);
 
            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();
 
        }
 
    });
 
  }
}

xml

<RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
 
        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />
 
        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />
 
    </RadioGroup>

for more detail visit this :Android getting value from selected radiobutton

Method : 2 Store selected data into String.

RadioGroup rg = (RadioGroup)findViewById(R.id.radioSex);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString(); 
Community
  • 1
  • 1
  • @Peter Phyo you have to used `Method : 2` to get selected data store into `String`. –  May 10 '16 at 05:45