Neelay Srivastava Neelay Srivastava - 3 months ago 10
Android Question

setting radio button for different task

I am trying to show radio group in a dialog and when the radio button is clicked it will set some data in shared preference. I am able to make toast when any button is clicked but I want to do something when different button is clicked .I tried setting Onclick in the radio button but it is not working .I am trying to learn from here
this is my dialog class

public Activity c;
public Dialog d;
public Button yes, no;
public RadioGroup radioGroup;
public RadioButton radioButton;

public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
Radioactive();
}

private void Radioactive() {
radioGroup = (RadioGroup) findViewById(R.id.radio);
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);




}


I just want to know how to do some thing when specific radio button is checked.

Answer

You can achieve the radio button click as follows.

 `radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // checkedId is the RadioButton selected RadioButton rb=(RadioButton)findViewById(checkedId); textViewChoice.setText("You Selected "+rb.getText()); //Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show(); } });`

hope it will help .

Comments