1.
Custom String
Open “res/values/strings.xml” file, add some custom
string for radio button.
File : res/values/strings.xml
<?xml
version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World,
MyAndroidAppActivity!</string>
<string name="app_name">MyAndroidApp</string>
<string name="radio_male">Male</string>
<string name="radio_female">Female</string>
<string name="btn_display">Display</string>
</resources>
2.
RadioButton
Open “res/layout/main.xml” file, add “RadioGroup“,
“RadioButton” and a button, inside the LinearLayout.
File : res/layout/main.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />
</LinearLayout>
Radio
button selected by default.
To make a radio button is selected by default, put android:checked="true" within the RadioButton element. In this case, radio option “Male” is selected by default.
To make a radio button is selected by default, put android:checked="true" within the RadioButton element. In this case, radio option “Male” is selected by default.
3. Code Code
Inside
activity “
onCreate()” method, attach a
click listener on button.
File : MyAndroidAppActivity.java
packagecom.mkyong.android;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.RadioButton;
importandroid.widget.RadioGroup;
importandroid.widget.Toast;
publicclassMyAndroidAppActivityextendsActivity{
privateRadioGroup radioSexGroup;
privateRadioButton radioSexButton;
privateButton btnDisplay;
@Override
publicvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
publicvoidaddListenerOnButton(){
radioSexGroup=(RadioGroup)findViewById(R.id.radioSex);
btnDisplay=(Button)findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(View v){
// get selected radio button from radioGroup
intselectedId=radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton=(RadioButton)findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(),Toast.LENGTH_SHORT).show();
}
});
}
}
4. Demo
Run
the application.
1.
Result, radio option “Male” is selected.

No comments:
Post a Comment