Android RadioButton – XIC Coimbatore

Android RadioButton –“How to select single elements value into the RadioButton in android”  from XIC-Coimbatore

Basically radiobutton’s using for the quiz, mcq, gender, etc,. From multiple choices we can select any one element/value.

In your ADT tool create a new Android Application name as RadioButtonExample.

Open activity_main.xml drag and drop the RadioGroup widget from Layouts tab.

Then only we can select any one item from the group.

Then open activity_main.xml page select the radiogroup widget and change the id as rg_ma_gender. And change the value for radiobutton’s as male and female.

Move tab to MainActivity.java page. Create a global object for the RadioGroup as rg, RadioButton rb, Button btn.

Inside the onCreate method use findviewbyid method to connect with the RadioGroup and Button widget xml page to the java page like follow

rg = (RadioGroup) findViewById(R.id.rg_ma_gender);

 

btn = (Button) findViewById(R.id.btn_click);

 

Now using click button get value of selected radio item value, so set onClickListener for the Button.

And mention the below code for getting selected item value. Using Toast display the value.

 

btn.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

 

int SelectedId=rg.getCheckedRadioButtonId();

 

rb=(RadioButton)findViewById(SelectedId);

 

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

}

});

 

The step are used to single value from multiple radio buttons. User when click the button on that time result will show the value.

 

Sample Demo Input :

Output Screen:

Select Female option and click the button. See the Output below.

For More Android Application Event’s kindly visit our Blog page.

Hope this blog inspires you to embrace change and grow. Whether you are an XPLORER or not, feel free to glance through our blogs and help us guide and support you in your voyage to success. Happy Learning!

 

0 responses on "Android RadioButton - XIC Coimbatore"

Leave a Message

Your email address will not be published. Required fields are marked *

© Xplore IT Corp 2017. All rights reserved. Designed by Web Design Company
Xplore WhatsApp Chat
Send via WhatsApp
X