I have a radio button group in Android which looks something like:
Choose Color:
I need to get selected radio button and also its value.
I have 4 radiobuttons in this manner within radiogroup rg
rb1a=(RadioButton)findViewById(R.id.rb1a);
rb1b=(RadioButton)findViewById(R.id.rb1b);
rb1c=(RadioButton)findViewById(R.id.rb1c);
rb1d=(RadioButton)findViewById(R.id.rb1d);
tv1=(TextView)findViewById(R.id.tv1);
next1=(Button)findViewById(R.id.next1);
rg=(RadioGroup)findViewById(R.id.rg);
// I have error after this line.please help
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
    }
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
    }
});
                you can test the radion button with the isChecked() function.
for ex:
if(radio1_red.isChecked())
{
       txtView.setText("Red button is checked");
}
Have a look at this Example .
You can also refer this Page - Form Stuff given in android-sdk pages.
Do this for getting selected radio button and also its value:
 private OnClickListener radio_listener = new OnClickListener() {
    public void onClick(View v) {
        // Perform action on clicks
        RadioButton rb = (RadioButton) v;
        Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
    }
};
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With