Is it possible to build DialogPreference from DialogFragment?
Asked Answered
M

1

9

I have implemented directory picker as a DialogFragment and now I would like to use it in DialogPreference. Is it possible? How to do it?

Mon answered 25/6, 2013 at 15:13 Comment(0)
D
0

It's not obvious to me what exactly you want to do, but I'm assuming you have a Fragment layout which you want to show on a Preference click. Here is the code for it:

preferences.xml

<PreferenceScreen ... >
    ...
    <Preference android:title="Dialog Fragment Displayer"
        android:key="dialog_preference"
        android:selectable="true"/>
    ...
</PreferenceScreen>

PreferenceActivity.java (or wherever your main settings control is)

final Activity activity = getActivity(); //replace with this if in an Activity
Preference p = findPreference("dialog_preference");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean OnPreferenceClick(Preference preference) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        View view = activity.getLayoutInflater().inflate(R.layout.your_fragment, null, false);
        builder.setView(view);
        builder.create().show();
    }
});
Dang answered 28/11, 2020 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.