Menus

Wednesday 4 January 2017

Android Radio Group and Radio Button Operations


What is Radio Group ?

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.

Example of using Radio Group

Create the layout of the Activity


<RadioGroup
    android:id="@+id/rg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/rad1"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/rad2"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
       android:id="@+id/rad3"
        android:checked="false" />

</RadioGroup>


* Radio Group orientation as two types are vertical and horizontal

Android:placing the radio buttons horizontally


android:orientation="horizontal"

Android:placing the radio buttons vertically

android:orientation="vertical"

Code the Activity

At this point we will show how we can handle the change of a radio button, that belongs to a RadioGroup.

package com.example.root.calendar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class EntriesActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private Button btnSaveall;
    private RadioButton rad1, rad2, rad3;

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


        radioGroup = (RadioGroup) findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

// find which radio button is selected

if(checkedId == R.id.rad1) { Toast.makeText(getApplicationContext(), "choice: Radio1 Selected",Toast.LENGTH_SHORT).show(); } else if(checkedId == R.id.rad2) { Toast.makeText(getApplicationContext(), "choice: Radio2 Selected",Toast.LENGTH_SHORT).show(); } else if(checkedId == R.id.rad3) { Toast.makeText(getApplicationContext(), "choice: Radio3 Selected",Toast.LENGTH_SHORT).show(); } } }); rad1 = (RadioButton) findViewById(R.id.rad1); rad2 = (RadioButton) findViewById(R.id.rad2); rad3 = (RadioButton) findViewById(R.id.rad3); btnSaveall = (Button)findViewById(R.id.btnSaveall); btnSaveall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int selectedId = radioGroup.getCheckedRadioButtonId();

// find which radioButton is checked by id

if(selectedId ==rad1.getId()) { Toast.makeText(getApplicationContext(),"Radio 1 Selected" ,Toast.LENGTH_SHORT).show(); } else if(selectedId == rad2.getId()) { Toast.makeText(getApplicationContext(),"Radio 2 Selected" ,Toast.LENGTH_SHORT).show(); } else if(selectedId == rad3.getId()) { Toast.makeText(getApplicationContext(),"Radio 3 Selected" ,Toast.LENGTH_SHORT).show(); } } }); } }

Now let’s have a look at the code above. When a checked radio button is changed in its group, OnCheckedChangeListener is invoked in order to handle this situation. The onCheckedChanged() method of this interface, includes the unique id of the radio button that was selected and caused the invoke of the callback.
In this example we will show you another way of selecting the choice information (for example when a button is pressed). This can be done through getCheckedRadioButtonId(), which is a public function of RadioGroup class. This method returns the unique id of the radio button that is chosen from the group. You can have a look at the code to see how you can handle both situations.
Of course Android system provides us a more dynamic way of changing and handling the attributes of the application views. As a prerequisite is to map every view with the unique id component of the XML. This can be done viafindViewById() method.



Radio Buttons Set checked state through code


You can use either radioButton.setChecked(true); or radiobutton.setSelected(true);

private RadioButton radA; //declare 
.
.
.
rad1 = (RadioButton) findViewById(R.id.rad1);
rad1.setChecked(true);



Hiding a RadioButton in Android


you can hide the particular radio button this way
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.INVISIBLE);
or if you use View Gone menas radio button hide with sapce
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.GONE);

Hiding a radio group

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.Visible);


Hi use like this.
RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            myRadioButton.setVisibility(View.INVISIBLE);
        }
    });
OR
  <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        android:visibility="invisible"/>

radioGroup1.setVisibility(View.GONE);
radioGroup2.setVisibility(View.GONE);
radioGroup3.setVisibility(View.GONE);
radioGroup2.setVisibility(View.VISIBLE);


How to set the background colour of a RadioButton?


        <RadioGroup
            android:id="@+id/eligibility"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="horizontal"
            android:layout_weight=".5" >

            <RadioButton
                android:id="@+id/eligibilityYes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:background="@drawable/checkbox_background"
                android:text="yes" />

            <RadioButton
                android:id="@+id/eligibilityNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_background"
                android:text="No" />
        </RadioGroup>

your checkbox_background.xml file put image in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false" android:drawable="@drawable/radio_rad" />
    <item android:state_checked="true" android:drawable="@drawable/radio_green" />

</selector>

Hi use like this:

Text color  - android:textColor="@color/white"
Backgroud - android:background="@color/red"


try this...
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            if(radioButton.getText().equals("yes")) {
                radioButton.setBackgroundColor(Color.GREEN);
            } else {
                radioButton.setBackgroundColor(Color.RED);
            }
        }
    });



How to check if a radiobutton is checked in a radiogroup in Android?


If you want to check on just one RadioButton you can use the isChecked function
if(radioButton.isChecked())
{
  // is checked    
}
else
{
  // not checked
}
and if you have a RadioGroup you can use
if (radioGroup.getCheckedRadioButtonId() == -1)
{
  // no radio buttons are checked
}
else
{
  // one of the radio buttons is checked
}