A RadioGroup class is used for set of radio buttons. If we check one radio button that belongs to a radio group, it automatically unchecks any previously checked radio button within the same group.
Following are the important attributes related to RadioGroup control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.
Attribute | Description |
---|---|
android:checkedButton | This is the id of child radio button that should be checked by default within this radio group. |
android:background | This is a drawable to use as the background. |
android:contentDescription | This defines text that briefly describes content of the view. |
android:id | This supplies an identifier name for this view. |
android:onClick | This is the name of the method in this View's context to invoke when the view is clicked. |
android:visibility | This controls the initial visibility of the view. |
In this example i will be demonstrating how to control the Radio Buttons in Radio Group.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity as done in old projects.
3. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i made two layouts separating two different types of radiobutton click event.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:baselineAligned="false" android:orientation="horizontal" android:padding="5dp" > <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:padding="5dp" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="@string/button_click" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/facebook" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/twitter" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/whatsapp" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hike" /> </RadioGroup> <Button android:id="@+id/click_here" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:padding="10dp" android:text="@string/click" /> </LinearLayout> <LinearLayout android:padding="5dp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" android:gravity="center" android:text="@string/radio_button_click" /> <RadioGroup android:id="@+id/radioGroup2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/facebook" /> <RadioButton android:id="@+id/radio5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/twitter" /> <RadioButton android:id="@+id/radio6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/whatsapp" /> <RadioButton android:id="@+id/radio7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hike" /> </RadioGroup> </LinearLayout> </LinearLayout> |
4. Now, add the following code in MainActivity.java. In this class we show the selected radio button name on button click and also show the selected radio button name on selecting radio button directly.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package com.radiobutton; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends Activity { RadioButton radioButton; RadioGroup radioGroup1, radioGroup2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Finding both radio group IDs radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2); // Implementing click listener on button findViewById(R.id.click_here).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Finding checked button ID from radio group int selectid = radioGroup1.getCheckedRadioButtonId(); // Finding radio button ID from selectid radioButton = (RadioButton) findViewById(selectid); // Showing a toast by displaying checked radio button text Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show(); } }); // Implementing checked change listener on radio group2 radioGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // Finding ID/Position of radio group child int pos = radioGroup2.indexOfChild(findViewById(checkedId)); // Now finding radio button radioButton = (RadioButton) findViewById(checkedId); // Applying switch case for all radio buttons and displaying // there position and text via Toast switch (pos) { case 0: Toast.makeText( getBaseContext(), "You have Clicked RadioButton " + pos + " and Title : " + radioButton.getText(), Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText( getBaseContext(), "You have Clicked RadioButton " + pos + " and Title : " + radioButton.getText(), Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText( getBaseContext(), "You have Clicked RadioButton " + pos + " and Title : " + radioButton.getText(), Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText( getBaseContext(), "You have Clicked RadioButton " + pos + " and Title : " + radioButton.getText(), Toast.LENGTH_SHORT).show(); break; } } }); } } |
5. Run the application and you will get the output as shown in video and you are done.
Thanks. 🙂
Subscribe to us and get the latest news.