Lieuwe Lieuwe - 2 months ago 7x
Android Question

Checkbox default value & OnCheckedChangeListener

This is not so much of a problem but I am trying to find a correct way of doing this.

I have the following situation:

public class SettingsDialogFragment extends DialogFragment implements OnCheckedChangeListener {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings, container);


CheckBox lBox1 = (CheckBox)view.findViewById(;


return view;

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {



The "problem" I have is that by calling
the onCheckChanged will already fire. I guess that when I inflate the layout - the
is initialised with a false setting and me changing that to true indeed is a

I could of course change the order and assign the listener after I set the initial value, but is there a way to inflate the layout whilst somehow passing the initial values for the various components? They are dynamic so I cannot fix the values to a particular value in the settings.xml



The code of the CheckBox looks something like this:

public void setChecked(boolean checked) {
    if (mChecked != checked) {
        mChecked = checked;

        // Avoid infinite recursions if setChecked() is called from a listener
        if (mBroadcasting) {

        mBroadcasting = true;
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
        if (mOnCheckedChangeWidgetListener != null) {
            mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);

        mBroadcasting = false;            

So basically you cannot use the method without firing events, unless you remove or disable the event handler before (or set them afterwards only).