Reputation: 2059
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:
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)?
Upvotes: 1
Views: 981
Reputation: 2790
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" />
Upvotes: 1