Connor S
Connor S

Reputation: 363

How do I change the text color of preferences in preference FRAGMENT?

I have read through dozens and dozens of threads explaining how to change text color of preference items in a PreferenceActivity. I do not have a preference activity. I am using PreferenceFragment with a PreferenceScreen in res/xml

Settings Fragment

public class SettingsFragment extends PreferenceFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences); // the settings XML here (not the layout)

    }
}

fragment_settings.xml

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/AppTheme2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="General">

        <CheckBoxPreference
            android:key="pref_example_checkbox"
            android:title="first box"
            android:summaryOn="This is enabled"
            android:summaryOff="This is disabled"
            android:defaultValue="false" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="Notifications">

        <SwitchPreference
            android:key="pref_enable_notifications"
            android:title="Notifications on/off"
            android:defaultValue="true" />

        <EditTextPreference
            android:key="pref_notification_time"
            android:title="Notification time"
            android:summary="Some shit about notifications"
            android:inputType="number"
            android:dialogMessage="Dialog message!"
            android:defaultValue="5" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="About">

        <Preference
            android:key="pref_about_licenses"
            android:title="Licenses" />

        <Preference
            android:key="pref_about_app_version"
            android:title="Version" />
    </PreferenceCategory>

</PreferenceScreen>

I tried adding android:textColor="@color/white" to all of the Preference items in the preferences.xml, but all of the text is still black. All of these threads talk about creating a new style, but NONE of them explain how to apply it to a preference fragment.

A preference activity will show up in the Manifest, where a style can be easily applied. But for a PreferenceFragment, there is no entry in the manifest. Also, it seems like there must be a faster way to apply text colors to the preference screen than creating an entire new style.

Some of the text is black, and some is orange (my colorAccent) Where is this data stored and how do I change it? preference screen

Upvotes: 1

Views: 980

Answers (2)

MArio
MArio

Reputation: 122

Add this to theme

///Preference Fragment Font Color

<item name="android:textColorPrimary">@color/white_font_color</item>
<item name="android:textColorSecondary">@color/nice_grey_dark</item>
<item name="android:textColorTertiary">@color/white_font_color</item>

Upvotes: 3

Emmanuel Loisance
Emmanuel Loisance

Reputation: 726

Check to override this in your style.xml

<item name="android:textColor">@color/color_text</item>
<item name="android:textColorPrimary">@color/color_text</item>
<item name="android:textColorSecondary">@color/color_text</item>

textColorSecondary seems to be used by setting's summary, textColor by setting's title, and textPrimary idk (:

Upvotes: -1

Related Questions