0% found this document useful (0 votes)
3 views

Practical 12

Practical

Uploaded by

gayatriksh25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical 12

Practical

Uploaded by

gayatriksh25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical no 12

xmlCode:

<?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:orientation="vertical"
android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single radio Button"
android:layout_gravity="center"
/>
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio button 1"

/>
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio button 2"
/>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radiobutton inside radio group"
android:layout_gravity="center"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MALE"
/>
<RadioButton
android:id="@+id/r4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEMALE"
/>

</RadioGroup>

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Show Selected" />
</LinearLayout>

Java code :

package com.example.pr12;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


RadioButton r1,r2,r3,r4;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r1=findViewById(R.id.r1);
r2=findViewById(R.id.r2);
r3=findViewById(R.id.r3);
r4=findViewById(R.id.r4);
b1=findViewById(R.id.b1);

b1.setOnClickListener(new View.OnClickListener(){

public void onChecked(View v)


{
String s="";
if(r1.isChecked())
s="R1 is checked";

Toast.makeText(MainActivity.this, "radio 1 is selected",


Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like