I just added PreferenceActivity to my project in Android studio (auto generated) and I noticed, that my preferences navigation has subscreens. Instead of simple list with header categories (all inside single activity window), I'm getting my headers as simple list, then I need to click on item to get content displayed.
I don't want it, I need only simple preference screen without any navigation, with headers displayed as category label.
How to do that?
Just modify your PreferenceActivity
code like this:
public class SettingsActivity extends AppCompatPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
preference.setSummary(stringValue);
}
return true;
}
};
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
XML File:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<PreferenceCategory
android:key="primary_category"
android:title="Category 1">
<EditTextPreference
android:key="coffee_file"
android:title="EditText 1"
android:defaultValue="Value of EditText 1"
android:selectAllOnFocus="true"
android:inputType="text"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>
<PreferenceCategory
android:key="secondary_category"
android:title="Category 2">
<EditTextPreference
android:key="gin_file"
android:title="EditText 2"
android:defaultValue="Value of EditText 2"
android:selectAllOnFocus="true"
android:inputType="text"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>
</PreferenceScreen>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.