0

Hi I want to create preferences in my application but I cannot use resources at all due to some dependency issues. I am able to do this using the below code:

public class DTMainActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setPreferenceScreen(defaultPref());
    setDependencies();


}

// The first time application is launched this should be read
private PreferenceScreen defaultPref() {
    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

    SwitchPreference dLogTracingEnablePref = new SwitchPreference(this);
    dLogTracingEnablePref.setTitle(R_Class.R_String.dLogTracingEnablePrefString);
    dLogTracingEnablePref.setDisableDependentsState(false);
    dLogTracingEnablePref.setChecked(true);
    dLogTracingEnablePref.setKey(R_Class.R_String.dLogTracingEnablePrefKey);
    root.addPreference(dLogTracingEnablePref);
}

I would want to do this using the new fragment based approach, without using the deprecated APIs like getPreferenceManager etc.. I can create all the other UI layout elements like linearlayout etc.. without any resources, but when it comes to preferences and PreferenceFragment class, all that is available is addPreferencesFromResource() which would need an XML. Can any one help me here please?

1 Answer 1

0

I managed to made it using a PreferenceFragment, without addPreferencesFromResource(), Instead I just created the PreferenceScreen like you just did and used

try using the bindPreferenceSummaryToValue, consider "p" being a PreferenceScreen with Preferences already added into it, (and also that has been created and configured previously)

PreferenceScreen p = createPreferences();//a method that creates a PreferenceScreen and add some preferences into it
this.setPreferenceScreen(p);
bindPreferenceSummaryToValue(p.findPreference("preference_key"));

I responded to someone with a similar problem here .. perhaps you can check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.