Checkbox Preference and Checking if its enabled or disable

Jaison Brooks picture Jaison Brooks · Jun 6, 2013 · Viewed 10.6k times · Source

Im having issues with the below code on making a checkbox preference, That by default is checked. My first activity is a simple splash screen, and simply before show my imageview thread i want to check if the checkbox has been disable if so then i want to intent directly to the main activity and by default i show my image thread, or in reversed order of that.

Currently my splashscreen is launching no matter if its checked or now, Any help would be greatly appreciated

XML

 <CheckBoxPreference
      android:title="@string/category_tools_startupscreen"
      android:summary="@string/category_tools_startupscreen_summary"
      android:key="boot_animation" android:order="5" android:enabled="true"/>

SplashScreen

   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    if (settings.getBoolean("boot_animation", true)) {
        setContentView(R.layout.splash_screen);
        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // changed from 5000 to 4000 11.29
                    while (waited < 3000) {
                        sleep(100);
                        waited += 100;
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Intent i = new Intent();
                    i.setClassName("com.example.app",
                            "com.example.app.MainActivity");
                    startActivity(i);
                    finish();
                }
            }
        };
        splashThread.start();
    }

    else {
        Intent i = new Intent();
        i.setClassName("com.example.app",
                "com.example.app.MainActivity");
        startActivity(i);
        finish();
    }

}

Settings

    final CheckBoxPreference checkboxPref2 = (CheckBoxPreference) getPreferenceManager().findPreference("boot_animation");

            checkboxPref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
                 public boolean onPreferenceChange(Preference preference, Object newValue) {
                if(newValue instanceof Boolean){
                        Boolean boolVal = (Boolean)newValue;
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("checked", boolVal);
                        editor.commit();
                    }
                    return true;
                }
            }); 

Answer

Hoan Nguyen picture Hoan Nguyen · Jun 6, 2013

In your splash screen

PreferenceManager.setDefaultValues(this, R.xml.your_setting_xml, false);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

if (settings.getBoolean("boot_animation", true)) {
.........

You do not even need the code you posted for the Settings