Question

Is it possible to dynamically add EditTextPreference to PreferenceCategory in PreferenceFragment? Or set the number of EditTextPreference and then create as many? I will be gratefull for any help

Was it helpful?

Solution

I am using a helper class to build EditTextPreferences, using that I can do:

Context context = getActivity();

PreferenceCategory statusCat = (PreferenceCategory) findPreference(context
            .getString(R.string.status_category));

EditTextPreference editTextPref = EditTextPrefBuilder.getBuilder(context)
                                 .setTitle("title")
                                 .setSummary("summary")
                                 .build();

statusCat.addPreference(editTextPref);

You can of course also just create the EditTextPreference without this helper class, I just find the code easier to read when using it:

public class EditTextPrefBuilder {

    private final Context context;

    private String title;
    private String summary;
    private boolean enabled;

    public static EditTextPrefBuilder getBuilder(Context context) {
        return new EditTextPrefBuilder(context);
    }

    EditTextPreference ep = null;

    public EditTextPreference build() {
        ep = new EditTextPreference(context);
        ep.setTitle(title);
        ep.setSummary(summary);
        ep.setEnabled(enabled);
        return ep;

    }

    private EditTextPrefBuilder(Context context) {
        super();
        this.context = context;
        this.title = "";
        this.summary = "";
        this.enabled = false;
    }

    public EditTextPrefBuilder setTitle(String title) {
        this.title = title;
        return this;
    }

    public EditTextPrefBuilder setSummary(String summary) {
        this.summary = summary;
        return this;
    }

    public EditTextPrefBuilder setEnabled(boolean enabled) {
        this.enabled = enabled;
        return this;
    }

}

Lastly an example of the PreferenceCategory showing the key used for findPreference:

<PreferenceCategory
    android:key="@string/status_category"
    android:shouldDisableView="false"
    android:title="@string/module_status" >
    <SwitchPreference
        android:defaultValue="true"
        android:enabled="false"
        android:key="@string/key_module_activated_basis"
        android:switchTextOff="@string/deactive"
        android:switchTextOn="@string/active" />

    <CheckBoxPreference
        android:defaultValue="false"
        android:dependency="@string/key_module_activated_basis"
        android:enabled="false"
        android:key="@string/status_checkbox"
        android:selectable="false"
        android:summary="@string/please_wait"
        android:title="@string/checking" />
</PreferenceCategory>

Edit:

If I understand the questions in the comments correctly, you can do this:

@Override public void onStart() {
    // update initial values
    updateSummaries();
    super.onStart();
}

@Override
public void onResume() {
    // listen for when preference values change
    PreferenceManager.getDefaultSharedPreferences(context)
            .registerOnSharedPreferenceChangeListener(this);

    super.onResume();
}

@Override
public void onPause() {
    PreferenceManager.getDefaultSharedPreferences(context)
            .unregisterOnSharedPreferenceChangeListener(this);
    super.onPause();
}

@Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    // for simplicity update all summaries, could also inspect the key to only update that single Preference
    updateSummaries();
}

private void updateSummaries() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // update the summary of the EditText
    String editSummary = prefs.getString("key_for_edittext");
    (EditTextPreference)findPreference("key_for_edittext").setSummary(editSummary);
}

This example sets summary, you can of course also set title if that is what you want to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top