user5289749
user5289749

Reputation:

Why am I not getting PreferenceCategory's summary even after declaring it in settings.xml file?

I'm developing a material design app & I have declared an xml file for SettingsActivity.

The problem I'm encountering is that the PreferenceCategory's android:summary tag is not showing any summary even after declaring it in settings.xml.

This is the screenshot of SettingsActivity.java (see: no summary here):

enter image description here

Here's SettingsActivity.java file's code:

public class SettingsActivity extends AppCompatActivity {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        SpannableString s = new SpannableString("Settings");
        s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();

    }

    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        public static final String SHARE_APP_PREFERENCE_KEY = "prefNotify";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            getActivity().setTheme(R.style.prefCategoryStyle);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);

        }

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                              String key) {
            if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
                // do something
            }
        }

    }

}

Here's activity_settings.xml file's code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/windowBackground"
                tools:context="com.abc.xyz.SettingsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:elevation="4dp"/>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_height="match_parent"/>

</RelativeLayout>

Here's settings.xml file's code:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:key="prefNotification"
        android:title="@string/prefNotification"
        android:summary="@string/pref_notify_summary">
        <CheckBoxPreference
            android:id="@+id/checkBoxPref1"
            android:key="checkBoxPref1"
            android:summary="@string/checkBoxPref1"
            android:defaultValue="false"/>
        <CheckBoxPreference
            android:id="@+id/checkBoxPref2"
            android:key="checkBoxPref2"
            android:summary="@string/checkBoxPref2"
            android:defaultValue="true"/>
        <CheckBoxPreference
            android:id="@+id/checkBoxPref3"
            android:key="checkBoxPref3"
            android:summary="@string/checkBoxPref3"
            android:defaultValue="false"/>
    </PreferenceCategory>
</PreferenceScreen>

Please let me know what is wrong here!

Thanks in advance.

Upvotes: 1

Views: 123

Answers (1)

Andrew Brooke
Andrew Brooke

Reputation: 12173

You'll need to make your own layout to show the title and summary.

<PreferenceCategory
    android:key="prefNotification"
    android:title="@string/prefNotification"
    android:summary="@string/pref_notify_summary"
    android:layout="@layout/custom_preference_category">

and create custom_preference_category.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@android:id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/listSeparatorTextViewStyle" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp" />
</LinearLayout>

Mess around with the layout to fit your needs

Upvotes: 1

Related Questions