Search code examples
androidsharedpreferencespreferenceslistpreference

How to combine preference and CardView


I am trying to make a setting menu with cardView and Preference, but I am not sure how to combine these two... Here's the screenshot and I want to move the ListPreference and PreferenceScreen entries into the setting cardView above. Here's the preference.xml The layout/setting_group is a cardview

<PreferenceScreen
    android:layout="@layout/setting_title_top"
    android:title = "Padding" />
<PreferenceScreen
    android:layout="@layout/setting_group">
</PreferenceScreen>
<ListPreference
    android:id="@+id/list"
    android:layout="@layout/setting_menu"
    android:dialogTitle = "title one"
    android:key = "key_1"
    android:title = "ListPreference"
    android:summary = "Summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ListPreference>

<PreferenceScreen
    android:layout="@layout/setting_menu"
    android:key="Key two"
    android:summary="Summary"
    android:title="PreferenceScreen" />
<PreferenceScreen
    android:layout="@layout/setting_title"
    android:title = "Another Setting" />

enter image description here


Solution

  • The PreferenceScreen is a container, you are opening and closing him with noting inside.

    You can create preferences like that:

     <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    
    
    
       <EditTextPreference
        android:capitalize="words"
        android:defaultValue="@string/default_value" <!--The initial state of the preference -->
        android:key="@string/pref_key" <!-- The key for retrieve the preference -->
        android:singleLine="true"
        android:title="@string/pref_title" />  <!-- The title who is showing above the preference -->
    
    
     </PreferenceScreen>
    

    Edit: Create a xml file to with the layout of your preferences and create and Activity, example:

     public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key))); // Is deprecated, but still a good option to no need change the values manually.
    
    
    }
    
    private void bindPreferenceSummaryToValue(Preference preference) {
        preference.setOnPreferenceChangeListener(this);
    
        onPreferenceChange(preference, PreferenceManager
                .getDefaultSharedPreferences(preference.getContext())
                .getString(preference.getKey(), ""));
    
    }
    
    
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        preference.setSummary(stringValue);
    
    
    
        return true;
    }
    }