Radio Group Button setOnCheckedChangeListener Java Android
This is how you can implement setOnCheckedChangeListener and create radio button groups in Java Android Studio:
You have some problem with radio group, you can new to Android development so now you will have much idea to how can handle it.
Android Radio Button Group and radioGroup.setOnCheckedChangeListener in JAVA ANDROID
In this article, we are going to learn how to work with setOnCheckedChangeListener method for radio buttons in android?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Create Empty Project
How to implement on Android — setOnCheckedChangeListener for Radio Buttons in android programmatically
Step 2 — “Add the following code to res/layout/activity_main.xml.“
I worked in simple relative layout method for only give basic example.
*In the below code, we have declared <RadioGroup as ”@+id/radioButton1” and ”@+id/radioButton2”.. and then ”@+id/radioButton6”.
RELATIVE LAYOUT FILE CODE IN XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yourname.yourandroidstuido.MainActivity"
android:padding="16dp"
android:id="@+id/relativeLayout"
android:orientation="vertical">
<TextView
android:id="@+id/turkeyfootball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="150dp"
android:gravity="center"
android:text="TURKEY FOOTBALL TEAM COLOURS" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="White"
/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black"
/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow"
/>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"
/>
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow"
/>
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
</RadioGroup>
<TextView
android:id="@+id/textViewJavaLearn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="550dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING" />
</RelativeLayout>
Step 3: MainActivity.java” java file − Add the following code to src/MainActivity.java
The following examples show how to use android.widget.RadioButton#setOnCheckedChangeListener() . These examples are extracted from open source projects
JAVA CODE IN MAIN ACTIVITY:
package com.yourname.yourpackagename;import android.graphics.Color;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RelativeLayout relativeLayout;
String toast ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radioButton1: Toast.makeText(MainActivity.this, "Besiktas Colour", Toast.LENGTH_SHORT).show();
relativeLayout.setBackgroundColor(Color.WHITE);
break;
case R.id.radioButton2: Toast.makeText(MainActivity.this, "Besiktas Colour", Toast.LENGTH_SHORT).show();
relativeLayout.setBackgroundColor(Color.BLACK);
break;
case R.id.radioButton3: Toast.makeText(MainActivity.this, "Fenerbahçe Colour", Toast.LENGTH_SHORT).show();
relativeLayout.setBackgroundColor(Color.YELLOW);
break;
case R.id.radioButton4: Toast.makeText(MainActivity.this, "Fenerbahçe Colour", Toast.LENGTH_SHORT).show();
relativeLayout.setBackgroundColor(Color.BLUE);
break;
case R.id.radioButton5: Toast.makeText(MainActivity.this, "Galatasaray Colour", Toast.LENGTH_SHORT).show();
relativeLayout.setBackgroundColor(Color.YELLOW);
break;
case R.id.radioButton6: Toast.makeText(MainActivity.this, "Galatasaray Colour", Toast.LENGTH_SHORT).show();
relativeLayout.setBackgroundColor(Color.RED);
break;
}
}
});
}
}
Review Application
In this tutorial we are simply adding setOnCheckedChangeListener() click event on radio group. With this app developer can easily do multiple type of task when each radio button is clicked or not. So here is the complete step by step tutorial for Add setOnCheckedChangeListener to radiogroup in android.
Step 4: Results and How to Work SEEK BAR Codes!
android java — RadioGroup and RadioButton example
A RadioButton is a widget which can be set to checked or an unchecked state. Once a RadioButton is checked you cannot uncheck it unless it’s present inside a RadioGroup.
A RadioGroup is a container that holds RadioButtons. At a time inside a RadioGroup, only one RadioButton can be set as checked.
A RadioButton is defined in the xml in the following manner:
In this lesson, you learnt Best Java code snippets using radioGroup.setOnCheckedChangeListener in java. import android.widget.RadioGroup;
How to use android.widget.RadioGroup in Android !
What is Radio Button
Interface definition for a callback to be invoked when the checked radio button changed in this group.
RadioGroup.OnCheckedChangeListener study finished.
If you have any question write comments I will reply.
Thanks for reading my blog.
Have a nice day.
If you need Google AdMob Alternatives you can check out Start Ad Platfrom.
Regards
5S JAVA AND KOTLIN LEARNING