Hello folks ! working with another UI widget in android application is very easy task. Radio Button is used to quick select and select the choice between two or three. So, just start with Radio Button coding parts . I will show you complete registration form using sqlite later on with my tutorial series. How to make a proper animated registration form I will show you later. But first you should learn UI widgets so that you can fit according to your need.
Tip – While using Radio Button you should use RadioGroup in xml ,that’s a good practice for being a developer .
So lets start with designing part . main.xml
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 |
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CHOICE IS YOURS" android:textSize="25sp"/> <RadioGroup android:id="@+id/group_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="Male" android:checked="true" /> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="female" /> </RadioGroup> <Button android:id="@+id/btnDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show me" /> </LinearLayout> |
Now , working with MainActivity I am going to give the file name is MyAndroidAppActivity.java.In the Java file I have just defined the below line i.e.
1 |
int selectedId = radioSex_Group.getCheckedRadioButtonId(); |
1 |
radioSex_Button = (RadioButton) findViewById(selectedId); |
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 |
package androidarena.radiobutton; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MyAndroidAppActivity extends Activity { private RadioGroup radioSex_Group; private RadioButton radioSex_Button; private Button btn_Display; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); checkbutton(); } public void checkbutton() { radioSex_Group = (RadioGroup) findViewById(R.id.group_radioSex); btn_Display = (Button) findViewById(R.id.btnDisplay); btn_Display.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // get selected radio button from radioGroup int selectedId = radioSex_Group.getCheckedRadioButtonId(); // find the radiobutton by returned id // you can do any operation here like send values on server or save values on sqlite radioSex_Button = (RadioButton) findViewById(selectedId); Toast.makeText(MyAndroidAppActivity.this, radioSex_Button.getText(), 2000).show(); } }); } } |
Ouput – This is the quick and simple Radio Button in android application. You guys can include these two lines as I above mentioned. and can have the access in your app. For more query and suggestions please comment here . 😛