0% found this document useful (0 votes)
55 views3 pages

Radio Button Assignment

The document contains XML and Java code for a radio buttons assignment. The XML code defines a linear layout with radio buttons for gender selection - male, female, other. The Java code initializes the radio buttons, sets onclick listeners to display Toast messages for the selected gender.

Uploaded by

NIGHT D3vil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

Radio Button Assignment

The document contains XML and Java code for a radio buttons assignment. The XML code defines a linear layout with radio buttons for gender selection - male, female, other. The Java code initializes the radio buttons, sets onclick listeners to display Toast messages for the selected gender.

Uploaded by

NIGHT D3vil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Radio Buttons Assignment

Muhammad Shehzaib Khalid


XML Code

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Butons Assignment"
android:textColor="#C70039"
android:gravity="center"
android:textSize="30dp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Gender"
android:layout_weight="1"
android:gravity="center"
android:textSize="15dp"
android:textColor="#000000"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_weight="1"
android:id="@+id/button1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_weight="1"
android:id="@+id/button2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"
android:layout_weight="1"
android:id="@+id/button3"/>

</RadioGroup>
</LinearLayout>

Java Code

package com.example.radiobuttons;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioButton button1;
RadioButton button2;
RadioButton button3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.findViewById(R.id.button1);
button2.findViewById(R.id.button2);
button3.findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Gender Male is Selected",
Toast.LENGTH_SHORT).show();

}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Gender Female is selected",
Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Gender Other is selected",
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like