Android: checkbox.isChecked() is always returning a false condition

Jeff S picture Jeff S · Oct 7, 2011 · Viewed 14.8k times · Source

I'm trying to code a checkbox into a help screen which is essentially a pop up view (an activity that's started by the main program) containing a ScrollView that has the help text, and OK button, and a checkbox that asks if you want the help screen to appear automatically at program start. Everything displays properly on the screen, and the checkbox toggles, when touched. However, when OK is pressed, and I test the state of the checkbox with .isChecked() I'm always getting a false. I'm sure it's something simple I've missed. XML file follows follows:

helpdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:background="#ffffff">

 <ScrollView android:id="@+id/ScrollView01"
 android:layout_width="wrap_content" android:layout_below="@+id/ImageView01"
 android:layout_height="300px">

 <TextView android:text="@+id/helpView" android:id="@+id/helpView"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:textColor="#000000"/>

</ScrollView>
<Button android:id="@+id/Button01" 
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:text="  OK  "
android:layout_below="@id/ScrollView01"/>

<CheckBox android:id="@+id/chkNoHelp" android:textColor="#000000"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:text="Don't Display at Startup"
android:layout_below="@id/Button01"        />  

</RelativeLayout>

HelpBox.java:

public class HelpBox extends Activity {

CheckBox checkBox;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //set up dialog
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.helpdialog);
    dialog.setTitle("Help");
    dialog.setCancelable(true);

    //set up text
    TextView text = (TextView) dialog.findViewById(R.id.helpView);
    text.setText(getString(R.string.help_text));

    //set up button
    Button button = (Button) dialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Boolean mDisplayHelp;

            setContentView(R.layout.helpdialog);

            checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
            if (checkBox.isChecked()) {
                mDisplayHelp = true;
            } else {
                mDisplayHelp = false;
            }
            finish();
        }
    });
    //now that the dialog is set up, it's time to show it    
    dialog.show();
}
}

Setting breakpoints on both "mDisplayHelp" lines always breaks at the 'false' branch regardless of whether the check box displays a green check or not when the OK button is pressed.

Thanks in advance.

Edit (10/10):

Its clear what I want to do is pick up information after the user exits the dialog, so I can sense the state of the checkbox and store it as a preference. For this I assume I have to test it in onDestory. So, I did that:

@Override
public void onDestroy() {
    Boolean mDisplayHelp;

    setContentView(R.layout.helpdialog);        
    checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
    if (checkBox.isChecked()) {
        mDisplayHelp = true;
    } else {
        mDisplayHelp = false;
    }

}

However, I'm still always coming up with FALSE as a result, regardless of whether the checkbox is display checked or off. In this instance, if I don;t include the setContentView, I get a NullPointerException on the isChecked.

Answer

Pedantic picture Pedantic · Oct 7, 2011

Why are you calling setContentView a second time? After the second time you call it, your checkbox is being reset to unchecked (the default state) and then you are immediately checking its state to set a flag.

Its not clear what your intention is here by inflating helpdialog.xml twice, once in a Dialog and once in your main Activity. It sounds like you want to use a dialog-themed activity here and only call setContentView() once in onCreate. It should behave as expected after that.