Get Started with Radio Button – UI Widget

radio_androidarena

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 .

Download Code

So lets start with designing part . 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" >
    
    <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.

int selectedId = radioSex_Group.getCheckedRadioButtonId();
radioSex_Button = (RadioButton) findViewById(selectedId);
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 – radio_button 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 . 😛

Leave a Reply

Your email address will not be published. Required fields are marked *