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

How To Check Radiobuttun With Options

The document describes how to create a radio button group with multiple options in an Android application. It includes an XML layout file defining a radio group with radio buttons for options A, B, C and D. The MainActivity class finds the radio group, sets an onCheckedChanged listener to display a toast message for the selected option. The code checks which radio button id was selected and displays the corresponding toast message.

Uploaded by

ilias ahmed
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)
50 views4 pages

How To Check Radiobuttun With Options

The document describes how to create a radio button group with multiple options in an Android application. It includes an XML layout file defining a radio group with radio buttons for options A, B, C and D. The MainActivity class finds the radio group, sets an onCheckedChanged listener to display a toast message for the selected option. The code checks which radio button id was selected and displays the corresponding toast message.

Uploaded by

ilias ahmed
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/ 4

How to check radiobuttun with options

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


<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">

<RadioGroup
android:id="@+id/G"
android:layout_width="match_parent"
android:layout_height="133dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="113dp"
android:text="A"
android:textSize="40sp" />

<RadioButton

android:layout_width="50dp"
android:layout_height="wrap_content"

android:layout_alignParentStart="true"
android:layout_alignParentTop="true"

android:text="B"
android:textSize="40sp" />

<RadioButton

android:layout_width="50dp"
android:layout_height="wrap_content"

android:layout_alignParentStart="true"
android:layout_alignParentTop="true"

android:text="C"
android:textSize="40sp" />

<RadioButton

android:layout_width="50dp"
android:layout_height="wrap_content"

android:layout_alignParentStart="true"
android:layout_alignParentTop="true"

android:text="D"
android:textSize="40sp" />

</RadioGroup>
</RelativeLayout>

Main.java

package com.example.androidapps.myapplicationre;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

RadioGroup G;

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

G.findViewById(R.id.G);
G.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case 1:
{
Toast.makeText( MainActivity.this,
"A",Toast.LENGTH_SHORT).show();
break;
}

case 2:
{

Toast.makeText(MainActivity.this,"B",Toast.LENGTH_SHORT).show();
break;
}

case 3:
{

Toast.makeText(MainActivity.this,"C",Toast.LENGTH_SHORT).show();
break;
}
case 4:
{

Toast.makeText(MainActivity.this,"C",Toast.LENGTH_SHORT).show();
break;
}
}
}
});
}
}
editeeted xml

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


<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">

<RadioGroup
android:weightSum="20"
android:id="@+id/G"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A"
android:textSize="40sp" />

<RadioButton
android:layout_weight="5"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:layout_alignParentStart="true"
android:layout_alignParentTop="true"

android:text="B"
android:textSize="40sp" />

<RadioButton

android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="C"
android:textSize="40sp" />

<RadioButton

android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="D"
android:textSize="40sp" />

</RadioGroup>
</RelativeLayout>

You might also like