Issue
I have two radio groups, each contains two radio buttons. I would like to wait until a button from each group has been selected, and then display a Toast. (E.g. if radio button 1 from group 1 AND radio button 3 from group 2 aree selected, display Toast message. I am not getting any error messages with the following, but no Toast is displaying:
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb2" />
</RadioGroup>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="228dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb3" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb4" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
JAVA CODE:
package com.example.rgapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
boolean checked = ((RadioButton) view).isChecked();
if (view.getId() == (R.id.rb1) & (view.getId() == (R.id.rb3)))
Toast.makeText(getApplicationContext(), "1 and 3 selected",
Toast.LENGTH_SHORT).show();
}
}
Solution
Try this below xml and java
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb2" />
</RadioGroup>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="228dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb3" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb4" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
MainActivity.java :
public class MainActivity extends Activity {
RadioButton rb1, rb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCustomClick(v);
}
});
}
public void onCustomClick(View view) {
boolean checked = ((RadioButton) view).isChecked();
if (rb1.isChecked() && rb3.isChecked())
Toast.makeText(getApplicationContext(), "1 and 3 selected",
Toast.LENGTH_SHORT).show();
}
}
It will display toast, if radiobutton1
from group 1 AND radiobutton3
from group 2 are selected.
Answered By - Android
Answer Checked By - Candace Johnson (JavaFixing Volunteer)