Online Tutorials & Training Materials | STechies.com
Register Login

Android Radio Button & Radio Groups Basics

|| || 0

Android Radio Button & Radio Groups Basics
Stechies

Difference between Checkbox and Radio Button

The basic difference between checkboxes and radio button is that whenever you want to allow the user to choose multiple options out of a list of options, you will generally use check boxes. But whenever you want to allow the user to choose only one option out of multiple options, then you can use radio buttons.Now let’s take a look at some examples so that we can understand this concept in more detail.

Add Radio Button and Radio Group

Step 1) So In the palette and there will be a container called radio button container or radio group, drag and drop the radio group in your virtual device's screen. The radio group is a container which is used to group radio buttons and whatever radio buttons we put inside this radio group, they are grouped together and the logic behind this grouping is that in this group, you will only be able to select one option out of multiple radio buttons.

Step 2) Now go to your palette and drag &  drop some radio buttons here. One, two and three radio buttons for three animals. And one button outside this radio group.

Step 3) Now in the properties for layout width of this radio group, change the layout width and height to wrap content so that the radio button do not take enough space.

Step 4) Now you can also change the names of these radio buttons according to your requirements. Also, define the

Step 5) Also define the id of this radio group selecting your radio group and going to the property called id and give an id to your radio group.

Java Codes for Radio buttons and Radio group

Step 1) First go to your MainActivity.java file.

Step 2) Now in MainActivity.java we first have to declare variables for three radio buttons and one button and one radio group

public class MainActivity extends ActionBarActivity{
	private static RadioGroup radio_g;
	private static RadioButton radio_dog,radio_cat,radio_cow;
	private static Button button_sbm;
}

 

Step 3) After declaring the variable we now have to create a method so that whenever we call this method OnCreate it can listen to button click event.This method will not take any argument.

Step 4) And inside the method we will cast our radio group first and then same for all other buttons.

public void onClickListenerButton(){
	radio_g = (RadioGroup)findViewById(R.id.rg_animals);
	button_sdm = (Button)findViewById(R.id.button);
}

 

Note: Let's do one more thing before calling this set onClick Listener for the button. Declare only one variable for radio button because we are going to need only one radio button because we are going to allow the user to select only one radio button. So we don’t require more variables for radio buttons and whatever radio button users select we are going to cast this to this radio b.

Step 6) Now we will set onClick Listener for buttons and inside this Onclick method, we have to also declare an integer for deciding or for getting the id from the group.

button_sbm.setOnClickListener(
	new View.OnClickListener(){
	@Override
	public void OnClick(View v){
		int selected_id = radio_g.getCheckedRadioButtonId();
		radio_b = (RadioButton)findViewById(selected_id);
	}
)

 

Step 7) So now we can call the Toast.makeText and in there, we can call three arguments.

Toast.makeText(mainActivity.this,
	radio_b.getText().toString(),Toast.LENGTH_SHORT).show();
)

 

Step 8) Now we are going to include this method in our onCreate method. So we will copy the name of the function or method here and paste it here and change it to German and paste it here.

@Override
protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	onClickListenerButton();
}

So now run the application and whenever we select any item from here, we will only be able to select one out of these items because remember we have enclosed these radio buttons inside the radio group and whenever you enclose these buttons inside the radio group, you will only be able to select one radio button at a time.

You can add multiple radio groups to group your radio button so you can add one more group, for example, choose your favourite anything and you will be able to select only one item out of this radio group also. So always group your radio buttons inside a radio group.So in this way, you can use radio buttons in your Android activity.


Related Articles