1

I want to include a toolbar in my settings page and followed this instruction: https://stackoverflow.com/a/27455330/2977288 (first part)

For some reason, the toolbar overlaps the content: enter image description here

I also tried to make the text color white: https://stackoverflow.com/a/26555178/2977288

But as you can see in the screenshot, the color remains black.

Here is the code I'm using:

settings_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

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

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

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        root.addView(bar, 0); // insert at top
        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <PreferenceCategory android:title="@string/pref_main">
        <CheckBoxPreference
            android:key="vibration"
            android:title="@string/pref_vibration"
            android:summary="@string/pref_summary_vibration"
            android:defaultValue="true" />
        <Preference
            android:key="show_wizard"
            android:title="@string/pref_wizard"
            android:summary="@string/pref_summary_wizard">
            <intent
                android:action="android.intent.action.VIEW"
                android:targetPackage="de.tum.dstrctrl"
                android:targetClass="de.tum.dstrctrl.activities.WizardActivity" />
        </Preference>
    </PreferenceCategory>

</PreferenceScreen>

Does anybody has an idea about one of the two issues (or both)?

Community
  • 1
  • 1
Zoker
  • 2,020
  • 5
  • 32
  • 53

1 Answers1

1

Please use the custom layout for the PreferenceActivity and in that layout include the toolbar and other stuff as suggested on How to add ToolBar in PreferenceActivity?.

Hope this helps.

UPDATE You can change the title color of Toolbar by creating custom style and then use that custom layout in toolbar

<style name="ActionBar" parent="Theme.AppCompat">
        <item name="android:background">@color/actionbar_background</item>
        <item name="android:titleTextStyle">@style/ActionBar.TitleTextStyle</item>
    </style>

    <style name="ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_title_text</item>
    </style>

settings_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    style="@style/ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
Community
  • 1
  • 1
Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
  • Why does it not work with this answer? http://stackoverflow.com/a/27455330/2977288 – Zoker Jan 26 '16 at 13:15
  • What's the purpose of `getFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit();` in `PreferenceActivity`, you have to add the line in `onCreate` as `addPreferencesFromResource(R.xml.preferences);` – Mustansar Saeed Jan 26 '16 at 13:24
  • I now solved it by using http://developer.android.com/guide/topics/ui/settings.html#Activity instead of fragments. So at least what you said... Any idea about the color? – Zoker Jan 26 '16 at 13:25
  • That's the thing I just shared that you don't have to use fragment. I tested your code at my end, Just removed the fragment thing and add the line `addPreferencesFromResource(R.xml.preferences);` it worked perfectly. No need of fragment – Mustansar Saeed Jan 26 '16 at 13:27
  • I just used the fragments, because Android Doc recommends it. Any idea about the text color? It's still balck – Zoker Jan 26 '16 at 13:28
  • Create the custom `PreferenceCategory` as suggested in http://stackoverflow.com/questions/11607302/how-to-change-text-color-of-preference-category-in-android – Mustansar Saeed Jan 26 '16 at 13:32
  • It's not the color of the category, but of the text on the toolbar. I followed this instructions, but still does not work: http://stackoverflow.com/a/26555178/2977288 – Zoker Jan 26 '16 at 13:34
  • Do you want to change the settings color? – Mustansar Saeed Jan 26 '16 at 13:35
  • Yes the color of the word "Settings" and of the arrow – Zoker Jan 26 '16 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101678/discussion-between-mustansar-saeed-and-zoker). – Mustansar Saeed Jan 26 '16 at 13:40