Reputation: 29
I am trying to add a summary to one of my list items. (Summary under 'Systemstandardeinstellung')
How can I do it? I haven't found any solution yet, the only thing I found was how to use to currently selected ListItem as the ListPreference summary.
This is what I have.:
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="basic_settings_category"
android:title="Grundeinstellungen"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="0"
android:key="@string/theme_preferences_key"
android:title="Designs"
app:entries="@array/themes_entries"
app:entryValues="@array/themes_values"
app:iconSpaceReserved="false" />
<SwitchPreference
android:key="night_mode"
android:title="Nachtmodus"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>
theme_res.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes_entries">
<item>Systemstandardeinstellung</item>
<item>Hell</item>
<item>Dunkel</item>
</string-array>
<string-array name="themes_values">
<item>@string/system_theme_preference_value</item>
<item>@string/light_theme_preference_value</item>
<item>@string/dark_theme_preference_value</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="app_name">TestApp</string>
<string name="settings">Settings</string>
<string name="openNavDrawer">Navigation Drawer Open</string>
<string name="closeNavDrawer">Navigation Drawer Close</string>
<string name="theme_preferences_key">theme_preferences_key</string>
<string name="notification_preferences_key"></string>
<string name="system_theme_preference_value">0</string>
<string name="light_theme_preference_value">1</string>
<string name="dark_theme_preference_value">2</string>
<string name="system_theme_description">Systemstandardeinstellung</string>
<string name="light_theme_description">Hell</string>
<string name="dark_theme_description">Dunkel</string>
</resources>
Upvotes: 2
Views: 377
Reputation: 1872
You can use androidx.preference.ListPreference
and app:useSimpleSummaryProvider
directly in XML:
<ListPreference
android:key="@string/setting_example_key"
android:title="@string/setting_example_title"
android:entries="@array/setting_example_labels"
android:entryValues="@array/setting_example_values"
app:useSimpleSummaryProvider="true"
android:defaultValue="@string/setting_example_default" />
Upvotes: 0
Reputation: 1333
Best chance is to use a dialog with custom items with a custom adapter.An easy way to do this is by using a spinner with spinnermode set to dalog and populate it with custom items.
in your activity layout add this spinner
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="@+id/test_spn"/>
Have your custom items class
public class custom_item {
public String name,description;
public int id;
public custom_item(String name, String description)
{
this.name=name;
this.description=description;
}
public custom_item(int id, String name, String description)
{
this.name=name;
this.description=description;
this.id=id;
}
}
In Your layout folder, have your custom item layout file (custom_item_layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:textStyle="bold"
android:id="@+id/title"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/description"
android:textSize="10dp"
android:text="description"
android:layout_marginLeft="10dp"/>
</LinearLayout>
Then your adapter class to do the adaptation (custom_adapter)
public class custom_adapter extends BaseAdapter {
ArrayList<custom_item> pref_items;
Context contxt;
public custom_adapter(Context contxt, ArrayList<custom_item> pref_items)
{
this.contxt=contxt;
this.pref_items=pref_items;
}
@Override
public int getCount() {
return pref_items.size();
}
@Override
public Object getItem(int position) {
return pref_items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView=null;
custom_item item= pref_items.get(position);
convertView= LayoutInflater.from(contxt).inflate(R.layout.custom_item_layout,null,false);
TextView title_=(TextView)convertView.findViewById(R.id.title);
TextView description=(TextView)convertView.findViewById(R.id.description);
title_.setText(item.name);
description.setText(item.description);
return convertView;
}
}
so with all things set ,just save either the id or the index of the item into a shared preference when selected. Put this in your activity's oncreate
Spinner my_spn=((Spinner)findViewById(R.id.test_spn));
final String sp_name="SharedPrefS_name";
SharedPreferences prefs = getSharedPreferences(sp_name, MODE_PRIVATE);
ArrayList<custom_item> items=new ArrayList<custom_item>();
items.add(new custom_item("Systemstandardeinstellung","Description for the first title (Systemstandardeinstellung)" ));
items.add(new custom_item("Hell","Description for the second title (Hell)" ));
items.add(new custom_item("Dunkell","Description for the third title (Dunkell)" ));
my_spn.setAdapter(new custom_adapter(this,items));
my_spn.setSelection(prefs.getInt("my_item_identifier", 0));
my_spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor saver = act.getSharedPreferences(sp_name, MODE_PRIVATE).edit();
saver.putInt("my_item_identifier", position);
saver.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Upvotes: 0