Practical No 12
Practical No 12
Q1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<LinearLayout
android:layout_width="407dp"
android:layout_height="487dp"
android:layout_marginTop="76dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Single Radio Buttons"
android:textAlignment="center"
android:textColor="#8E8E8E" />
<RadioButton
android:id="@+id/rd_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 1" />
<RadioButton
android:id="@+id/rd_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 2" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button Inside Radio
Group"
android:textAlignment="center"
android:textColor="#8E8E8E"
android:layout_marginTop="10dp"/>
<RadioGroup
android:id="@+id/rd_grp"
android:layout_width="match_parent"
android:layout_height="201dp">
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:layout_marginStart="140dp"/>
</LinearLayout>
<EditText
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical12;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.*;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button show = findViewById(R.id.show);
RadioButton rd1 = findViewById(R.id.rd_btn1);
RadioButton rd2 = findViewById(R.id.rd_btn2);
RadioGroup rd_grp = findViewById(R.id.rd_grp);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer str = new StringBuffer();
if(rd1.isChecked())
str.append(rd1.getText().toString()+ " ");
if(rd2.isChecked())
str.append(rd2.getText().toString()+ " ");
RadioButton checked_btn =
findViewById(rd_grp.getCheckedRadioButtonId());
if(checked_btn != null)
str.append(checked_btn.getText().toString()+ " ");
Toast.makeText(MainActivity.this,str.toString()+" is
selected",Toast.LENGTH_LONG).show();
}
});
}
}