Search code examples
javaandroidpreferenceandroid-sharedpreferences

Does shared preferences/preference activity allow for enabling/disabling of fragments inside the page?


I have an activity that extends preference activity, to be a settings page. Inside the settings page is an edit text and three different listviews. I want to hardcode the input of the edit text so that when the right "password" (the hardcoded string) is entered, it enables the previously hidden and disabled listviews. Is this possible to happen in same time and how?

 private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    password = (EditText)findViewById(R.id.adminpasswordsetting);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferencesFragment()).commit();

}

public static class MyPreferencesFragment extends PreferenceFragment{
    @Override
    public void onCreate(final Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

    }
}
private void setAdminSettings(){
    //set the password
    if(password.getText().toString().equals("helloworld")){
    //SharedPreferences.Editor editor = preferences.edit;
    }
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){

}

and my xml preference is just one edittextpreference and three listpreferences


Solution

  • This code probably will not run 'as is' ... so do not copy-paste it.. but I am thinking something alog following lines of code (just as an idea)

    public class PrefsActivity extends PreferenceActivity {
    
        public PrefsActivity() {
            // TODO Auto-generated constructor stub
        }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        ListPreference     lp1 = (ListPreference) findPreference(getString(R.string.lp1)); 
        CheckBoxPreference cb2 = (CheckBoxPreference) findPreference(getString(R.string.cb2)); 
        lp1.setEnabled(false);
        cb2.setEnabled(false);
        EditTextPreference etp1 = (EditTextPreference) findPreference(getString(R.string.mypassword)); 
    
        String  mypassword =PreferenceManager.getDefaultSharedPreferences(this).getString(getString(R.string.mypassword), "0");
    
      if ("THE_PASSWORD".equals(mypassword)) {
            lp1.setEnabled(true);
            cb2.setEnabled(true);
        }
    
    }
    
    }