Reputation: 4627
In preference activity I suply listpreference from xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="lang"
android:title="@string/LangTitle"
android:summary="@string/LangSummary"
android:defaultValue="en"
android:entries="@array/entries_lang"
android:entryValues="@array/entryvalues_lang"
android:dialogTitle="@string/LangDialogTitle"
/>
</PreferenceScreen>
At first is shown preference screen with setting tittle and description. When I click it dialog with availible options is shown.
Is there a way to show only that dialog? Is there a direct call to it?
Upvotes: 1
Views: 1656
Reputation: 4627
For all who may need it: There is no direct call, but it can be rebuilded easly. Following DialogFragment
create the exact same dialog as ListPreference
public class LanguagePreferenceDialog extends DialogFragment {
private CharSequence[] mEntries;
private CharSequence[] mEntryValues;
private String mValue;
private boolean mValueSet;
SharedPreferences prefs;
private int mClickedDialogEntryIndex;
@Override public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mEntries = getResources().getStringArray(R.array.entries_lang);
mEntryValues = getResources().getStringArray(R.array.entryvalues_lang);
mValue = prefs.getString("lang", "en");
}
@Override public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setTitle(getString(R.string.LangDialogTitle));
dialog.setPositiveButton(null, null);
mClickedDialogEntryIndex = getValueIndex();
dialog.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, selectItemListener);
return dialog.create();
}
private int getValueIndex() {
return findIndexOfValue(mValue);
}
public int findIndexOfValue(String value) {
if (value != null && mEntryValues != null) {
for (int i = mEntryValues.length - 1; i >= 0; i--) {
if (mEntryValues[i].equals(value)) {
return i;
}
}
}
return -1;
}
DialogInterface.OnClickListener selectItemListener = new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
if (mClickedDialogEntryIndex != which) {
mClickedDialogEntryIndex = which;
mValue = mEntryValues[mClickedDialogEntryIndex].toString();
prefs.edit().putString("lang", mValue).commit();
}
dialog.dismiss();
}
};
}
Upvotes: 5
Reputation: 2493
We can't directly call ListPreference
without inflating preference xml file. Preference
should always be inside PreferenceScreen
. And we have to inflate the whole xml file inorder to make them available on UI. You will have to show a dialog box with a ListView in it.
Upvotes: 0