How to disable a RadioGroup until checkbox is checked - android

I have a radio group which I do not want to user to be able to select any of the buttons until a particular checkbox is selected within my app. If the checkbox is unticked then this disables the radio-group. How do I go about doing this.

The real trick is to loop through all children view (in this case: CheckBox) and call it's setEnabled(boolean)
Something like this should do the trick:
//initialize the controls
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1);
//set setOnCheckedChangeListener()
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
//basically, since we will set enabled state to whatever state the checkbox is
//therefore, we will only have to setEnabled(checked)
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(checked);
}
}
});
//set default to false
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}

If you have just a few radio buttons, better way would be to setClickable(false) for all children
radiobutton1.setClickable(false);
radiobutton2.setClickable(false);
radiobutton3.setClickable(false);

RadioGroup cannot be disabled directly, we have to loop through the radio button and setEnabled as false.
// To disable the Radio Buttons of radio group.
for (int i = 0; i < radioGroup.getChildCount(); i++) {
radioUser.getChildAt(i).setEnabled(false);
}

If you want to implement the same for Kotlin, Here's my code->
where rgNotificationType is the radio group name.
for (i in 0 until rgNotificationType.childCount) {
(rgNotificationType.getChildAt(i)).isEnabled = false
}

You can use the onCheckedChangeListener on your CheckBox and use the method setEnabled on your RadioGroup.
Best wishes,
Tim

By using kolin's extensions:
fun RadioGroup.setChildrenEnable(enable: Boolean) {
for (i in 0 until this.childCount) {
this.getChildAt(i).isEnabled = enable
}
}
And in your code you can just call the function like this:
radioGroup.setChildrenEnable(false)

Kotlin Solution
for (index in 0..radio.childCount - 1)
radio.getChildAt(index).isEnabled = false

Take actions according to the state of the checkbox and set the radiogroup accordingly.
Assuming that you have a radio-group named radiogroup you can enable or disable the radiogroup by
radiogroup.setEnabled(true);
Add a OnCheckedChangeListener() to your checkbox.

Related

How to group radio buttons programmatically in Java/Kotlin?

I have a custom view for radio buttons in the XML layout file with ConstraintLayout. I need to group those radio buttons. But when I try to envelop that ConstraintLayout with the Radio group, it does not group the buttons and all options are getting selected.
RadioButtons need to be the direct children of a RadioGroup in order for the grouping functionality to work. A RadioGroup is just a special LinearLayout that implements the grouping functionality for all the children added that are RadioButtons. You can simply implement your own grouping functionality by adding the RadioButtons to a List and then implementing OnCheckedChangeListener and setting it on each of the RadioButtons as follows:
private final List<RadioButton> radioButtons = new ArrayList<>();
private int checkedId;
private void setRadioButton(RadioButton radioButton) {
radioButtons.add(radioButton);
radioButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener =
new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedId = buttonView.getId();
}
for (RadioButton radioButton : radioButtons) {
if (radioButton.getId() != checkedId) {
radioButton.setChecked(false);
}
}
}
};
you can set false the checked of icon in the defult running like this code in layout
xml
android:checked="false"
and in coding you can call binding.rdiobutton.setonclicklistenert{
binding.btnclick.setonclicklistener)
Use the following code in kotlin
radioGroup.check(id)
First, you write the name of the radio group, then instead of the ID, put the ID of the radio button,
For example, it is like this in my project
rg_licenseType.check(R.id.rb_three)

Activate or de activate recyclerview items

I have a fragment which contains a recyclerview and a floating action button. In recyclerview, each row contains a radio button set as disabled by default. my requirement is I need to enable these radio buttons in every row upon floating action button click event. Is there any solution?
Thanks in advance..
OK... I got a solution.
here is my code.
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i <= adapter.getItemCount(); i++) {
View childVIew = recyclerView.getChildAt(i);
if (childVIew != null) {
RadioButton radioYes = childVIew.findViewById(R.id.radio_yes_my_health_info);
radioYes.setEnabled(true);
RadioButton radioNo = childVIew.findViewById(R.id.radio_no_my_health_info);
radioNo.setEnabled(true);
}
}
fab.setImageResource(R.drawable.ic_save_white_24dp);
recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0);
}
});
Now on FAB button click, all the radio buttons inside my recyclerview will be enabled and I can click to change the state of every radio button. But here I have got a problem. When I click FAB button, all the radio buttons inside the recyclerview will be activated but when I scroll the recyclerview all those radio buttons will be disabled or deactivated again. Please give me a solution for this.
You can have a flag in your adapter like
boolean isActivated = false;
public void setActivated(boolean activated) {
isActivated = activated;
}
and in your onBindViewHolder() method you can check for the flag and set the state of your flag accordingly.
In the OnClickListener of your FAB, call
adapter.setActivated(true);//true or false as per your requirement
adapter.notifyDataSetChanged();
This should work.
You can try looping through every view when you click the button, like so:
for (int i = 0; i < adapter.getItemCount(); i++) {
View view = recycler.getLayoutManager().findViewByPosition(i);
view.setSelected(true); //Select item
notifyItemChanged(i); //Notify item changed
}

Android -get radio button id based on value

I want to highlight right and wrong option when user clicks a button after checking an radio button. If that option is right, highlight. Else highlight right option.
Is there any way to get radio button id in a group based on its value? Or Do I need to use switch case?
I searched enough but not able to find out what I need.
Edit
I have simple layout which contains one question, 4 choices, one button. User check a radio button and click the check button. If user selects wrong option, highlight the correct option. I know what is correct option by value.
choice1, choice2, choice3, choice4 are four radio buttons. User checks choice3. But choice2 is correct. How do I select choice2 by value in this radio group.
group.getRadioButtonId("choice2")
Anything similar to this?
You could iterate through the children of your RadioGroup to get the required one:
int count = radioGroup.getChildCount();
for (int i = 0 ; i < count; i++) {
RadioButton button = (RadioButton) radioGroup.getChildAt(i);
if (button.getText().equals("choice2")) {
int id = button.getId(); // the ID you're looking for
}
}
You Can check like this
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int rbId = group.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(rbId);
switch (group.getId()) {
case R.id.radiogroupID:
if (rb.getText().toString().equalsIgnoreCase("right")) {
// your logic
} else {
//your logic
}
}
}
you can use these line. its working for me
int checkedId = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(checkedId);
int radioId = radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
First check which option is true. hear choice 2 OK. set by default is checked true and than check user checked value . if user select other option than display message and pass true option we already checked true
Create an int array with all the ids of the radiobutton
int[] rb_IDs=new int[]{R.id.rb1,R.id.rb2,R.id.rb3};
Then inside your button click :
RadioGroup rg=(RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i=0;i<rb_IDs.length;i++)
{
RadioButton rb=(RadioButton)findViewById(rb_IDs[i]);
if (rb.getText().toString().equals("YOUR CORRECT ANSWER")&&!rb.isChecked())
{
//
//Do your thing
//
//ID of the correct answer Radiobutton is
int id=rb_IDs[i];
//HighLighting the answer
rb.setBackgroundColor(Color.parseColor("#00ff00"));
break;
}
}
}
});

How to get all of unchecked radio buttons in android

I have 16 Radio Group in my layout and i have 40 Radio Button . I want to get which Radio Button is unchecked in Radio Groups. I want to know how can i know is there any unchecked Radio Button in my layout thanks
You should probably group all of your buttons like so:
RadioGroup rg = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> radioButtonsList = new ArrayList<>();
for(int i = 0; i < rg.getChildCount(); ++i) {
RadioButton b = rg.getChildAt(i);
if(b.isChecked()) radioButtonsList.add(b);
}
Do it for all of your groups and you'll have all your unchecked buttons in a list.
Also you can use:
int checkedRadioButtonId = rg.getCheckedRadioButtonId()
to get only checked button's id.
ArrayList<RadioGroup> radioGroupList = new ArrayList<RadioGroup>();
RadioGroup group1 = (RadioGroup)findViewById(...);
RadioGroup group2 = (RadioGroup)findViewById(...);
.
.
RadioGroup group16 = (RadioGroup)findViewById(...);
radioGroupList.add(group1);
radioGroupList.add(group2);
.
.
radioGroupList.add(group16);
and later you can check which is checked or not with this
for(RadioGroup radioButtonGroup:RadioGroupList){
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
}
or if it's the RadioButtons you are interested in then add them in an ArrayList the same way and loop in that list like this
for(RadioButton radioButton:radioButtonList){
boolean isChecked = radioButton.isChecked();
}
You can check the state of radio buttons by using the isChecked() method.
This question has already been answered here:
How to check if "Radiobutton" is checked?

How to change the background color of a RadioGroup at runtime?

I am trying to generate radio button in a radio Group dynamically. I have a radioGroup having three radioButtons. On runtime I want to put a validation that if no radio Button is checked, set a background color of radioGroup to red indicating warning.
For the purpose I am using
public String validate() {
if (radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()) != null){
return "SUCCESS";
} else {
radioGroup.setBackgroundResource(R.drawable.red_border);
return "failure";
}
}
but here the background is not changing for radioGroup i have tried similar approach for editText and DatePicker in all that it's working.
We can do this using looping through radioGroup children
for (int i = 0; i < radioGroup.getChildCount(); ++i) {
((RadioButton) radioGroup.getChildAt(i))
.setBackgroundResource(R.drawable.red_border);
}
This will loop through each and every children of RadioGroup and like this we can achieve the desired functionality

Categories

Resources