TrinityTuts Tips

Get Selected RadioButton from RadioGroup in Android

In android we always use RadioGroup for RadioButton and get value of the  selected radio button. check below code to know how we do this

RadioGroup radiogroup =  (RadioGroup) findViewById(R.id.groupid);
Button bt = (Button) findViewById(R.id.btnDisplay);

bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // get selected radio button from radioGroup
            int selectedId = radiogroup .getCheckedRadioButtonId();

            // find the radio button by returned id
            RadioButton radioButton = (RadioButton) findViewById(selectedId);

           Toast.makeText(MainActivity.this,
                radioButton.getText(), Toast.LENGTH_SHORT).show();
        }
});

You can use above code inside your onCreate method or any other method. In this first i find RadioGroup Id and after when user click on button we find Selected Radio button from group and save its id in a variable and find the selected radio button value.

🙂