0

I'd like to use pre-defined shared preferences to set the value of a checkBoxPreference, and to set the shared preference with the checkBoxPreference. I tried this code, but checkboxPref always ends up being null even though I know "pre_definied_shared_prefs" exist.

final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("pre_defined_shared_prefs");


    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Boolean b = (Boolean) newValue;

            Intent i = getIntent();
            Integer show_num = i.getIntExtra("show_num", -1);
            SettingsManager s = new SettingsManager();
            s.setShowNotification(show_num, b, getApplicationContext());

            return true;
        }
    });

Why would this be and how can I fix it?

HighLife
  • 4,218
  • 7
  • 40
  • 56
  • Can you paste your preference xml file? Do you have android:key attribute for your checkboxpref? – VendettaDroid Jan 20 '13 at 04:24
  • Yes I do, but I don't want to use that attribute. I have a function that sets "pre_defined_share_prefs" depending on what calls it. – HighLife Jan 20 '13 at 04:27
  • so what you saying is your key is set by some function but its not set in xml. Am I correct? Is your function called before you are trying to get your preference. – VendettaDroid Jan 20 '13 at 04:30
  • Before the `setOnPreferenceChangeListener` I would do `final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference(function_that_outputs_pre_created_key());`. Can I do this? – HighLife Jan 20 '13 at 04:34
  • It depends how your function outputs the key. The key has to be a real key which has been set to the preference either in XML or by calling setKey() on Preference in Java code. If the key is just a string which is not actually attached to preference than it will not make sense. – VendettaDroid Jan 20 '13 at 04:36
  • Look at these, http://developer.android.com/reference/android/preference/Preference.html and http://developer.android.com/reference/android/preference/PreferenceGroup.html#promethods – VendettaDroid Jan 20 '13 at 04:37
  • I have an I'm still relatively confused. I'd like to create a class with a method that takes in an id and creates a related boolean preference and key and a method that takes in the id and returns the related key to attach to the check box. Whats the best way to do this? – HighLife Jan 20 '13 at 04:41
  • Btw, are you calling addPreference to add a preference after creating it in your method? – VendettaDroid Jan 20 '13 at 04:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23032/discussion-between-vendettadroid-and-highlife) – VendettaDroid Jan 20 '13 at 04:48
  • I have edited my answer which explains how to add a new preference. – VendettaDroid Jan 20 '13 at 04:51

1 Answers1

0

My preference.xml:

<CheckBoxPreference
          android:title="Show Call UI"
          android:defaultValue="true"
          android:summary="Show Call Interface when clicking call button"
          android:key="checkboxPref" />

Rest code:

final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkboxPref");

    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Log.d("MyApp", "Pref " + preference.getKey() + " changed to " + newValue.toString());       
            return true;
        }
    }); 

Here is the example on how to create new checkbox preference and add it to the group you wished or look at this

public class MyPreferenceActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.my_preference_activity);

        //fetch the item where you wish to insert the CheckBoxPreference, in this case a PreferenceCategory with key "targetCategory"
        PreferenceCategory targetCategory = (PreferenceCategory)findPreference("targetCategory");

        //create one check box for each setting you need
        CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
        //make sure each key is unique  
        checkBoxPreference.setKey("keyName");
        checkBoxPreference.setChecked(true);

        targetCategory.addPreference(checkBoxPreference);
    }
}
Community
  • 1
  • 1
VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
  • Is there a way to attach this programmatically created checkBoxPreference in place of one created in xml so that I can keep the title and description that was set in xml? – HighLife Jan 20 '13 at 05:18
  • you can also setTitle and setSummary in the Java code. Look at this link for list of functions which could be used, http://developer.android.com/reference/android/preference/Preference.html – VendettaDroid Jan 20 '13 at 06:30
  • I think, no need to include preference.xml file. – Ashokchakravarthi Nagarajan Dec 05 '13 at 13:49