RadioButton in Android

In this tutorial we will learn about Radio Button in Android. Radio buttons are kind of buttons which has only two states checked or unchecked the same as CheckBox expects with little differences. Radio Buttons are generally used with another widget which in RadioGroup. Suppose a RadioGroup contain two Radio Buttons it means we can select only one RadioButton at a single time. We can not select both Radio Buttons once. For Example, we have Radio Group of two Radio Buttons Male or Female in our signup form in this case user can only select one radio button to mention their gender. In this tutorial, we will learn how to RadioButtons in RadioGroup in the XML layout file and how to get selected RadioButton from this RadioGroup in Java file.

RadioButton in Android Learn Android for free


RadioButton Example
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:text="Gender" android:textSize="18sp"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:id="@+id/gender"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" android:id="@+id/male"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" android:id="@+id/female"/> </RadioGroup> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:id="@+id/submit"/> </LinearLayout>


We got this output after writing this code in out XML file..

Radio Button in Android


RadioButton Example

public class MainActivity extends AppCompatActivity {

    RadioGroup gender;
    Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gender=(RadioGroup)findViewById(R.id.gender);
        submit=(Button)findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (gender.getCheckedRadioButtonId()){
                    case R.id.male:
                        Toast.makeText(getApplication(),"You Selected Male",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.female:
                        Toast.makeText(getApplication(),"You Selected Female",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

    }
}




Radio Button in Android





Share this tutorial with your friends on your social Media..

RadioButton in Android RadioButton in Android RadioButton in Android RadioButton in Android



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com