Introduction
In this blog, we will learn how to implement Radio Button and Check Box in Android.
Radio Buttons:
Radio Buttons allow the user to select only one option from a number of options. You should use Radio Buttons when the user needs to select only one option. When the user selects a radio button, any previously selected radio button in the same group gets deselected.
Check Box:
Checkboxes allows the user to select one or more options from a list of options. You should represent each checkbox in a vertical list. You can choose one or more options from the list.
Example in Real Time
Radio Buttons:
If you want to select what the Gender is at the time of registration, you can use Radio Buttons like below:
Check Box:
If you want to select multiple items from the list of items, you can use Checkbox like below, Here, we are selecting more than one country name from the Countries list:
Classes & Methods
For Radio Buttons:
Classes – RadioGroup
RadioButton
Button
Methods – onCreate(){..}
setOnClickListener(new OnClickListener()){.. }
For Check Box:
Classes – CheckBox
Button
Methods – onCreate() {..}
setOnClickListener(new OnClickListener()){..}
Example
In this example, we will learn how to implement Check Box and Radio Button in Android.
Step 1: Create a new Project.
Step 2: Give a name AndroidRadioButtonCheckBoxExample .
Step 3: Add a Layouts in res > layout > activity_radiobutton.xml and activity_checkbox.xml.
activity_radiobutton.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<RadioGroup android:id="@+id/selectGender" android:layout_below="@+id/txtView" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/Male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_male" android:checked="true"/> <RadioButton android:id="@+id/Female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/role_administrator"/> </RadioGroup> |
activity_checkbox.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<CheckBox android:id="@+id/country_india" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/india" /> <CheckBox android:id="@+id/country_china" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/china"/> <CheckBox android:id="@+id/country_us" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/us" /> |
strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AndroidRadioButtonCheckBoxExample</string> <string name="action_settings">Settings</string> <string name="hello_world">RadioButton and CheckBox Demo</string> <string name="str_male">Male</string> <string name="str_female">Female</string> <string name="str_submit">Submit</string> <string name="india">India</string> <string name="china">China</string> <string name="us">United States</string> </resources> |
MainRadioActivity.java
1 2 3 4 5 |
private RadioGroup selectRole; private RadioButton btn; private Button display; |
Add a Code in onCreate() {..} :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); selectRole = (RadioGroup) findViewById(R.id.selectRole); display = (Button) findViewById(R.id.display); display.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int selectedId = selectRole.getCheckedRadioButtonId(); btn = (RadioButton) findViewById(selectedId); Toast.makeText(MainRadioActivity.this,btn.getText(), Toast.LENGTH_LONG).show(); } }); } |
MainCheckBoxActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
private CheckBox countryIndia, countryChina, countryUS; private Button btnResult; Add a code in OnCreate() : @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); countryIndia = (CheckBox) findViewById(R.id.country_india); countryChina = (CheckBox) findViewById(R.id.country_china); countryUS = (CheckBox) findViewById(R.id.country_us); btnResult = (Button) findViewById(R.id.btnResult); btnResult.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (countryIndia.isChecked()) { Toast.makeText(MainCheckBoxActivity.this, "You selected India..!", Toast.LENGTH_LONG).show(); } else Toast.makeText(MainCheckBoxActivity.this, "Try selecting India..!", Toast.LENGTH_LONG).show(); }} } |
Output :
Hope this blog was informative to get you started with Radio Buttons and Check Boxes. For more details, you can contact us at support@acadgild.com
Leave a Reply