Practical 11
Practical 11
Exercise :-
1). Write a program show the following output First two radio buttons are without using
radio groop and next two radio buttons are using radio group. Note the changes
between these two. Also toast which radio button has been selected.
Code :-
XML:Code
<?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 ">
<TextView android:id="@+id/textv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single Radio Buttons"
android:textColor="#000000 "
android:visibility="visible"
android:layout_centerHorizo ntal="true"
android:textSize="20dp"
android:layout_marginTop=" 20dp"
/>
<RadioButton
android:id="@+id/radioButt on1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:layout_marginTop=" 80dp"
android:textSize="40dp"
/>
<RadioButton
android:id="@+id/radioButt on2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:layout_marginTop=" 40dp"
android:layout_below="@+id/radioButton1"
android:textSize="40dp"
/>
<RadioGroup android:layout_below="@+id/radioButton2"
android:layout_width="matc h_parent"
android:layout_height="200d p"
android:layout_marginTop="50dp"
android:id="@+id/radioGrou p">
<TextView android:id="@+id/textvr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button inside RadioGroup"
android:textSize="20dp"
android:textColor="#000000 "
android:visibility="visible"
android:layout_marginTop=" 25dp"
/>
<RadioButton
android:id="@+id/male" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:checked="true" android:textSize="40dp"
/>
<RadioButton
android:id="@+id/female" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Female"
android:checked="true"
android:textSize="40dp"
/>
</RadioGroup>
<Button android:id="@+id/Select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:layout_centerHorizo ntal="true"
android:layout_below="@+id/radioGroup"
/>
</RelativeLayout> Java Code:
package com.example.exp12;
import androidx.appcompat.app.App CompatActivity;
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
{
RadioButton radgender,rad1,rad2;
RadioGroup radSelect;
String gender;
Button select;
@Override protected voidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstance State);
setContentView(R.layout.acti vity_main);
radSelect=findViewById(R.id.radioGroup);
rad1=findViewById(R.id.radi oButton1);
rad2=findViewById(R.id.radi oButton2);
{
int radioID= radSelect.getCheckedRadioB uttonId();
radgender=findViewById(rad ioID);
gender=radgender.getText().t oString().trim();
if(rad1.isChecked())
{
Toast.makeText(this, "Radio button 1 is selected", Toast.LENGTH_SHORT).show();
select=findViewById(R.id.Sel ect);}
if(rad2.isChecked())
{
select.setOnClickListener( new View.OnClickListener()
{
Public void onClick(View v)
{
select();
}
}
);
}
public void select()
Toast.makeText(this, "Radio button 2 is selected", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "Gender :" + gender, Toast.LENGTH_SHORT).show();
}
}
Output :-
Subject :- Mobile Application Developmen Subject Code :- 22617
Name :- Shivam Sainath Korpakwad Batch :- CO 6 IA Roll No. 24
Exercise :-
2). Write a program to place Name, Age and Mobile Number centrally on the display
screen using Absolute layout.
Code :-
Output :-