KaliMa KaliMa - 1 year ago 100
Android Question

How can I launch a DialogFragment from clicking a preference item?

In my preferences.xml file I have things like

<?xml version="1.0" encoding="utf-8"?>

android:title = "Random Title"
android:key = "random_category">


And I have a PreferenceFragment:

public class PrefFragment extends PreferenceFragment {

public void onCreate(final Bundle savedInstanceState) {


I want to add an item to the category where if you click on it, it calls SomeFunc() (which launches a DialogFragment and from there I know how to save preference values using a helper I wrote), but I don't know how to include just a regular clickable Preference that triggers this function.

Answer Source

To add on click event

    Preference button = findPreference(preferenceKey);
    button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            // Do some stuff here
            return true;

It is also worth checking out Android Studio's default preference page (New -> Activity -> Settings Activity) - it's quite neet but not too straightforward.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download