0

I have multiple checkbox in recycler view adapter.and one button in parent view.i want to enable button if any checkbox checked and disable button if all checkboxes are unchecked.how can i do that,

Holder.checboxone.setOnCheckedChangeListener(new 

 CompButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompButton compButton, 
    boolean b) {
                item.setChecked(b);

                checkboxStatus(b);

            }
        });


private void checkboxStatus(boolean isChecked) {

    if (isChecked) {
        ((MainActivity)context).getButton().setEnabled(true);

    } else  {
        ((MainActivity)context).getButton().setEnabled(false);
    }
}

According to my code i can enable button when i click ay checkbox,but disable function not working properly,if i unchecked single checkbox also button will disable,i want to disable button after all checkbox uncheck only

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28

3 Answers3

2

Inside Activity (implement MyInterface)

int selectedCount = 0; // Global variable

@Override
public void checkboxListener(boolean isSelected){
    if (isSelected)
        selectedCount++;
    else
        selectedCount--;
    if (selectedCount > 0)
        button.setEnabled(true);
    else
        button.setEnabled(false);
}

Interface~~!!

public interface MyInterface {
    void checkboxListener(boolean isSelected);
}

Call this interface from inside the Checkbox.onCheckedChangeListener(); inside the adapter.

UPDATE

Inside your adapter:

Change this method.

private void checkboxStatus(boolean isChecked) {
    myInterface.checkboxListener(isChecked);
}

You may be using a constructor too for your adapter, change that to:

public void MyAdapter(Context c, Data d, MyInterface myInterface){
    ...
    this.myInterface = myInterface;
}

Inside Activity:

public class MyActivity extends Activity implements MyInterface { ...

and when you initialize your adapter, do this:

MyAdapter myAdapter = new MyAdapter(this /* context */, data /* whatever is your data */, this);

Add the code from the first block (of my answer) inside your activity and you are good to go!!!

Geet Choubey
  • 1,069
  • 7
  • 23
0

I hope that I'm not too late.

var checker = document.querySelectorAll("#checkme");
var connect = document.getElementById("connect");

checker.forEach(function (element) {
  element.onchange = function () {
    connect.disabled = true;
    checked = document.querySelectorAll("#checkme:checked");
    if (checked.length == checker.length) {
      connect.disabled = false;
    }
  };
});
<script src="https://getuikit.com/assets/uikit/dist/js/uikit-icons.js"></script>
<script src="https://getuikit.com/assets/uikit/dist/js/uikit.js"></script>
<link href="https://getuikit.com/assets/uikit/dist/css/uikit.css" rel="stylesheet"/>
<div class="uk-container uk-margin-large-top">
  <form>
    <fieldset class="uk-fieldset">

      <legend class="uk-legend">All Boxes must be checked</legend>
      <p>Button will be enabled once all boxes checked</p>

      <div class="uk-margin uk-grid-small uk-child-width-1-1 uk-grid">

        <label><input id="checkme" class="uk-checkbox" type="checkbox"> A</label>
        <label><input id="checkme" class="uk-checkbox" type="checkbox"> B</label>
        <label><input id="checkme" class="uk-checkbox" type="checkbox"> B</label>
        <label><input id="checkme" class="uk-checkbox" type="checkbox"> C</label>
        <div class="uk-margin-small-top">
          <button id="connect" class="lab-search-btn uk-button uk-button-secondary lab-darkblue" uk-toggle="target: #phoneconnect" disabled><i uk-icon="check"></i> Agree to A, B, C, D </button>
        </div>

    </fieldset>
  </form>
  </div>

Please find a link to the codesnipet as well as codepen I created with a working example: https://codepen.io/team/lfprofessors/pen/dyVjXBv

Hercules S.
  • 363
  • 3
  • 13
-1

In the function check whether any checkbox is checked using a loop. Set a flag if anyone is checked. Use this flag to enable or disable the button.

References:

Xyz
  • 5,955
  • 5
  • 40
  • 58
Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28