RadioButton RadioGroup Java Android example

RadioButton RadioGroup Android example source code for Android developer
Example for *.xml files

       <RadioGroup android:id="@+id/idRadio_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:orientation="vertical">
 
            <RadioButton android:id="@+id/idRadio_1"
                android:text="@string/textLabel_1"/>
            <RadioButton android:id="@+id/idRadio_2"
                android:text="@string/textLabel_2"/>
            <RadioButton android:id="@+id/idRadio_3"
                android:text="@string/textLabel_3"/>
 
        </RadioGroup>

Example for *.java files

// import
import android.widget.RadioGroup;
// get handle of RadioGroup
RadioGroup  mRadioGroup = (RadioGroup) findViewById(R.id.idRadio_group);
 
// which RadioButton is selected for example in some function body
         int nUnits = 10; // decimetry
    	 int nIdRadio = mRadioGroup.getCheckedRadioButtonId();
    			if(nIdRadio == R.id.idRadio_1) nUnits = 1; // metr
    			else if(nIdRadio == R.id.idRadio_2) nUnits = 10; // decimetr
    			else if(nIdRadio == R.id.idRadio_3) nUnits = 100; // cm
    			else if(nIdRadio == R.id.idRadio_4) nUnits = 1000; // mm
 
 
// listener for RadioGroup Java Android example
       mRadioGroup.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {
                        Log.v("Selected", "New radio item selected: " + checkedId);
                        recordNewUIState();
                    }
                });
This entry was posted in Android Examples Code and tagged , , , , . Bookmark the permalink.


Comments are closed.



17,665 views