In this video it shows the code to implement the Radio Buttons in a Radio Group in your Android App’s Java code in Android Studio.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.radiobuttons;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
public void radioGroupOnclick(View view){
TextView textView = findViewById(R.id.textView);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
int intRadioButtonID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(intRadioButtonID);
switch (radioButton.getText().toString()){
case "Alpha":
textView.setText("Selected Button is " + radioButton.getText().toString());
break;
case "Beta":
textView.setText("Selected Button is " + radioButton.getText().toString());
break;
case "Charlie":
textView.setText("Selected Button is " + radioButton.getText().toString());
break;
case "Delta":
textView.setText("Selected Button is " + radioButton.getText().toString());
break;
default:
textView.setText("Default Button selected");
}
}
}
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.586" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="343dp"
android:layout_height="266dp"
android:layout_marginStart="58dp"
android:layout_marginTop="29dp"
android:onClick="radioGroupOnclick"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Alpha" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Beta" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Charlie" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delta" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="match_parent"
android:layout_height="59dp"
android:checked="true"
android:text="Default" />
</RadioGroup>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="112dp"
android:layout_marginTop="44dp"
android:onClick="radioGroupOnclick"
android:text="RadioGroup Selection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots: