Code example for CheckBoxPreference

Methods: setKeysetSummarysetTitle, setChecked

0
    	strSavedPassword = preferences.getString("master_password", null);
    	bolAutoLoggout = preferences.getBoolean("auto_loggout", true);
    	//set saved values 
    	editUsername.setText(strSavedUsername);
    	editPassword.setText(strSavedPassword);
    	autoLoggout.setChecked(bolAutoLoggout);    	
    } 
     
    private void SaveSettings() { 
    	if (autoLoggout.isChecked() == false) {
    		Toast.makeText(getBaseContext(), "Auto Close disabled. Please be sure to close vault when done or else the vault stays open.", Toast.LENGTH_LONG).show();
    	} 
    	 
    	//check for removing password 
    	if (removeLogin.isChecked() == false) {
    		try { //get data from form 
    			strSavedUsername = editUsername.getText().toString();
        	} catch (Exception e) {
        		strSavedUsername = "";
        	} 
        	try { //get data from form 
    			strSavedPassword = editPassword.getText().toString();
        	} catch (Exception e) {
        		strSavedPassword = "";
        	} 
        	 
        	if (strSavedUsername.equalsIgnoreCase("")) {
        		Toast.makeText(getBaseContext(), "WARNING: Master user name is blank. This is not recommended", Toast.LENGTH_LONG).show();
        	} 
        	if (strSavedPassword.equalsIgnoreCase("")) {
        		Toast.makeText(getBaseContext(), "WARNING: Master password is blank. This is not recommended", Toast.LENGTH_LONG).show();
        	} 
    	} 
    	else { //make null to remove 
    		strSavedUsername = null;
    		strSavedPassword = null;
    		Toast.makeText(getBaseContext(), "WARNING: Master user name/password have been removed. This is not recommended", Toast.LENGTH_LONG).show();
    	} 
    	 
    	bolAutoLoggout = autoLoggout.isChecked();
    	 
    	//save pref 
        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME,  MODE_WORLD_WRITEABLE);
        SharedPreferences.Editor editor = preferences.edit();
		editor.putString("master_username", strSavedUsername);
		editor.putString("master_password", strSavedPassword);
		editor.putBoolean("auto_loggout", bolAutoLoggout);
		editor.commit(); //Save changes
		 
		Toast.makeText(getBaseContext(), "Settings Saved", Toast.LENGTH_SHORT).show();
    } 
     
    private PreferenceScreen createPreferenceHierarchy() {
        // Root 
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
         
        // Dialog based preferences 
        PreferenceCategory masterInfoPrefCat = new PreferenceCategory(this);
        masterInfoPrefCat.setTitle("Master Username/Password");
        root.addPreference(masterInfoPrefCat);
        //Username edit 
        editUsername = new EditTextPreference(this);
        editUsername.setDialogTitle("Username:");
        editUsername.setKey("edittext_preference");
        editUsername.setTitle("Username");
        editUsername.setSummary("Change master username");
        masterInfoPrefCat.addPreference(editUsername);
        //password edit 
        editPassword = new EditTextPreference(this);
        editPassword.setDialogTitle("Password:");
        editPassword.setKey("edittext_preference");
        editPassword.setTitle("Password");
        editPassword.setSummary("Change master password");
        masterInfoPrefCat.addPreference(editPassword);
         
        //remove master login 
        removeLogin = new CheckBoxPreference(this);
        removeLogin.setTitle("Remove");
        removeLogin.setSummary("Removes master username/password");
        masterInfoPrefCat.addPreference(removeLogin);
                 
        //Title of this settings  
        PreferenceCategory closeVaultPrefCat = new PreferenceCategory(this);
        closeVaultPrefCat.setTitle("Close Vault Settings");
        root.addPreference(closeVaultPrefCat);
         
        autoLoggout = new CheckBoxPreference(this);
        autoLoggout.setKey("toggle_preference");
        autoLoggout.setTitle("Auto Close");
        autoLoggout.setSummary("Automatically close the vault on exit");
        closeVaultPrefCat.addPreference(autoLoggout);
 
        return root;
    } 
}