-2

Is there a way to check if CheckBox is checked from user

private android.widget.CheckBox.OnCheckedChangeListener checkboxListener = new android.widget.CheckBox.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    }

UPDATE

I solved this by implementing custom CheckBox class which wraps CheckBox and holds boolean value fromUser, also implemented custom listener, and overloading setChecked(boolean checked, boolean fromUser).

`public class PaCheckBox extends CheckBox {

private static boolean fromUser;

public PaCheckBox(Context context) {
    super(context);
}

public PaCheckBox(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public PaCheckBox(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void setChecked(boolean checked, boolean fromUser){
    setChecked(checked);
    PaCheckBox.fromUser = fromUser;
}

@Override
public void setChecked(boolean checked) {
    super.setChecked(checked);
    PaCheckBox.fromUser = true;
}

@Override
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
    super.setOnCheckedChangeListener(listener);
}

public static class PaOnCheckBoxChangeListener implements android.widget.CheckBox.OnCheckedChangeListener {



    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked, boolean fromUser) {

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        onCheckedChanged(buttonView,isChecked,PaCheckBox.fromUser);
    }
}

} `

Arman
  • 25
  • 7
  • You can get check status using `checkBox.isChecked();` – hrskrs Feb 02 '16 at 12:32
  • I don't want the state of CheckBox, I want to get if it's triggered from user, by callingSetChecked(), isChecked will be true – Arman Feb 02 '16 at 12:41

6 Answers6

0
private android.widget.CheckBox.OnCheckedChangeListener checkboxListener = new android.widget.CheckBox.OnCheckedChangeListener() {
    @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
 {
      if(isChecked)
      {


> do what ever you want


       }
}
krishna
  • 301
  • 1
  • 8
  • I guess you didn't got the question :) I don't want the state of CheckBox, I want to get if it's triggered from user, by callingSetChecked(), isChecked will be true – Arman Feb 02 '16 at 12:40
0

you are going with right code you need to put just condition.

    private android.widget.CheckBox.OnCheckedChangeListener checkboxListener = new      android.widget.CheckBox.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked)
        Log.e("Log","user checked box");
    else
        Log.e("Log","user Unchecked box");
    }
Vasant
  • 3,475
  • 3
  • 19
  • 33
  • I guess you didn't got the question :) I don't want the state of CheckBox, I want to get if it's triggered from user, by callingSetChecked(), isChecked will be true – Arman Feb 02 '16 at 12:40
0

it is an interesting question which has generated some confusion. I believe what you are asking for is similar to the SeekBar fromUser boolean whch denotes whether the user has clicked the screen or whether the checkbox has been altered programatically. A useful example of this requirement is when propogating a listview with checkboxes. As the View is current, the values are required to be set either by a holder of by entering the relevant data. An OnClickListener will be triggered regardless as to whether the user has touched the srceen as a deliberate selection. Whereas in the case of the SeekBar the boolean fromUser allows the programmer to make a choice.... as you have done with your wrapper. I am new to java and though, I am sure there must be a solution apart from your wrapper, I havent found one yet... will let you know if I do.

spleen
  • 11
  • 2
0

Just an idea, that I find has worked, use OnClickListener. It is only called when a user touches the control. It is not called if app changes check state.

    findViewById(R.id.switchFound).setOnClickListener(new View.OnClickListener () {
        @Override
        public void onClick(View v) {

            boolean isChecked = ((Switch) v).isChecked();

        }
    });
Kenneth Argo
  • 1,697
  • 12
  • 19
-1

boolean isChecked in onCheckedChanged() represents exactly that. If its true then CheckBox is checked and if false then unchecked.

private android.widget.CheckBox.OnCheckedChangeListener checkboxListener = new android.widget.CheckBox.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked)
            // user has check the checkbox
        else
           // user has uncheck it
}

From official document

 public abstract void onCheckedChanged (CompoundButton buttonView, boolean isChecked)

Called when the checked state of a compound button has changed.

Parameters

buttonView - The compound button view whose state has changed.

isChecked - The new checked state of buttonView.

Note: I would highly encourage you to read the documentation to understand anything you have doubts about before asking for help.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • I mean, we can trigger checked, programmatically, when initing, and then isChecked will be true, but I want to track if user has pressed check or uncheck – Arman Feb 02 '16 at 12:34
  • @Arman: That is why you can use this callback method and see if `isChecked` is true or false. If its true the user has check the `CheckBox` and if false then user has unchecked it. – Rohit5k2 Feb 02 '16 at 12:35
  • I guess you didn't got the question :) I don't want the state of CheckBox, I want to get if it's triggered from user, by callingSetChecked(), isChecked will be true – Arman Feb 02 '16 at 12:38
  • @Arman: You should really update the question. Here you will find how to do it. http://stackoverflow.com/questions/9129858/how-can-i-distinguish-whether-switch-checkbox-value-is-changed-by-user-or-progra – Rohit5k2 Feb 02 '16 at 12:39
  • @Arman: Isn't is the one I post the link of? If it works for you then all right. – Rohit5k2 Feb 02 '16 at 13:27
-1

Try like this ,

 checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    //your code here  
                    //this will be true here by default you need not write checkBox.setChecked(true);
                } else {
                    //not checked by user
                    //this will be false here by default you need not write checkBox.setChecked(false);
                }
            }
        });
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • 1
    And how you get if checkbox is checked by user, and not by call setChecked() from here? – Arman Feb 02 '16 at 12:51
  • by restoring the state of checkbox and calling setChecked() on checkbox object, this listener will be triggered and you want be able to distinguish between user pressed and restore call, do you get me? – Arman Feb 02 '16 at 12:58
  • you have to store state of checkbox and when user press , you have to check the status and logically you have to maintain status of checkbox and from that you have to differentiate between user pressed and restore call@Arman – Amit Vaghela Feb 02 '16 at 13:04