Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
58 views4 pages

Prog RadioButton

The document contains code for an Android application with a radio button group that allows the user to select a subject. It includes an XML layout file defining the user interface with text, radio buttons for subjects, and submit/clear buttons. The Java code handles setting up the radio button group, clearing selections, and displaying Toast messages with the selected subject on submit or change.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Prog RadioButton

The document contains code for an Android application with a radio button group that allows the user to select a subject. It includes an XML layout file defining the user interface with text, radio buttons for subjects, and submit/clear buttons. The Java code handles setting up the radio button group, clearing selections, and displaying Toast messages with the selected subject on submit or change.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

activity_main.

xml

<RelativeLayout
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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Subject ?"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="20dp"/>

<RadioGroup
android:layout_marginTop="50dp"
android:id="@+id/groupradio"
android:layout_marginLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id1"
android:text="DBMS"
android:textSize="20dp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id2"
android:text="C/C++ Programing"
android:textSize="20dp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id3"
android:text="Data Structure"
android:textSize="20dp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id4"
android:text="Algorithms"
android:textSize="20dp"/>
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginTop="200dp"
android:layout_marginLeft="180dp"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
/>

</RelativeLayout>

MainActivity.java

package in.edu.vpt.radiobuttondemo;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

private RadioGroup radioGroup;


Button submit, clear;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

submit = (Button)findViewById(R.id.submit);
clear = (Button)findViewById(R.id.clear);
radioGroup = (RadioGroup)findViewById(R.id.groupradio);

radioGroup.clearCheck();
radioGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int
checkedId)
{
RadioButton radioButton =
(RadioButton)group.findViewById(checkedId);
Toast.makeText(MainActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});

submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId == -1) {
Toast.makeText(MainActivity.this, "No answer has been
selected", Toast.LENGTH_SHORT).show();
}
else {

RadioButton radioButton =
(RadioButton)radioGroup.findViewById(selectedId);
Toast.makeText(MainActivity.this, radioButton.getText(),
Toast.LENGTH_SHORT).show();
}
}
});

clear.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
radioGroup.clearCheck();
}
});
}
}

Output

You might also like