user5289749
user5289749

Reputation:

Why PreferenceCategory's title & checkBox color is so light & how can I change it?

I'm developing a material design app & I have declared a xml file for Settings option. I have done this task successfully, but after running & opening the SettingsActivity from menu, it is appearing like this:

enter image description here

The problem here is that the title which is 'Notification' & the checkBox's color is very light making them difficult to be seen.

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 = "pref_key_share_app";

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

            // 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"
                tools:context="com.xxx.abc.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">
    <CheckBoxPreference
        android:id="@+id/prefNotifyCheckBox"
        android:dependency="prefNotification"
        android:key="prefNotify"
        android:summary="@string/pref_notify_summary"
        android:defaultValue="true"/>
    </PreferenceCategory>
</PreferenceScreen>

As I'm new to Android Development, I'm unable to figure out the problem here.

Please let me know.

Thanks in advance.

Upvotes: 2

Views: 825

Answers (1)

PPartisan
PPartisan

Reputation: 8231

PreferenceFragment uses the value stored in colorAccent of your app's theme to style widgets and titles by default. To resolve it, create a custom theme in your styles.xml file, and set the colorAccent attribute to an alternate colour:

<style name="theme_name" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/another_color</item>
    <item name="colorAccent">@color/yet_another_color</item>
</style>

To apply it to your PreferenceFragment, call getActivity().setTheme(R.style.theme_name) in onCreate() of your PreferenceFragment.

Upvotes: 7

Related Questions