Javascript Ajax Android Html PHP Example Quiz New MORE
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

RadioButton with Examples Android


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="100dp"
        android:textSize="18dp"
        android:text="Select Gender"
        android:textStyle="bold"
        android:id="@+id/txtView"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/rdGroup"
        android:layout_below="@+id/txtView">
        <RadioButton
            android:id="@+id/rdbMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="100dp"
            android:text="Male"
            />
        <RadioButton
            android:id="@+id/rdbFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="100dp"
            android:text="Female" />
    </RadioGroup>
    <Button
        android:id="@+id/getBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:background="#bf00ff"
        android:textColor="#FFFFFF"
        android:minHeight="30dp"
        android:layout_below="@+id/rdGroup"
        android:text="Submit" />
</RelativeLayout>


MainActivity.java


package com.example.radiobuttonexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
    RadioButton male, female;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        male = (RadioButton) findViewById(R.id.rdbMale);
        female = (RadioButton) findViewById(R.id.rdbFemale);
        Button btn = (Button) findViewById(R.id.getBtn);
    }

}