I use a very simple style to format my Toolbar, I use android:elevation to display a shadow under the Toolbar like this:
<style
name="AppToolbarTheme"
parent="AppTheme">
<item name="android:background">#color/colorPrimary</item>
<item name="android:titleTextColor">#android:color/white</item>
<item name="android:elevation">4dip</item>
</style>
And I'm applying it simply like that:
<Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppToolbarTheme"/>
The shadow displays under the toolbar like I want, howerver it is also somehow applied to the title of the toolbar which looks very weird:
Remove <item name="android:elevation">4dip</item> from style and put it in toolbar xml
<Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppToolbarTheme"
android:elevation="4dp"/>
I was looking at this and helped me to identify my mistake, which I think it is the same as yours:
You are using style properties (Widget) in a theme. Theme are used to define global properties such as colorOnSurface, colorSurface, primaryColor, etc. Here you are using:
<item name="android:background">#color/colorPrimary</item>
<item name="android:titleTextColor">#android:color/white</item>
<item name="android:elevation">4dip</item>
which are specific to a toolbar.
I recommend to define a style like below (you can set whatever parent you want as long as it is Widget.whatever.Toolbar)
<style name="Widget.Toolbar" parent="Widget.MaterialComponents.Toolbar.Surface">
<item name="elevation">4dp</item>
</style>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.Toolbar"/>
What I am doing at the moment is using appbar, so you can apply elevation there instead:
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:theme="#style/Theme.AppBarTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
<style name="Theme.AppBarTheme">
<item name="elevation">6dp</item>
<item name="toolbarStyle">#style/Widget.Toolbar</item>
</style>
<style name="Widget.Toolbar" parent="Widget.MaterialComponents.Toolbar.Surface">
<item name="android:titleTextColor">#android:color/white</item>
</style>
You use
android:theme="#style/AppToolbarTheme"
in your toolbar and in that case it add elevation to nested components as well 'cause you define a theme.
Use
style="#style/AppToolbarTheme"
instead.
Related
My issue is, that AppBarLayout, together with toolbar is appearing not on top of the screen, thus blocking the view of the included content. It can be seen in the image. I am sure I am just missing something here.
Also, here is the xml code
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search" />
EDIT: with my theme set to NoActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
it looks like this:
In res/values/styles set your style to NoActionBar theme, like:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Now, you should lose your regular ActionBar and you can work with your toolbar.
EDIT: What OP tried to acomplish was position included layout below AppBar layout in CoordinatorLayout. That's accomplished by adding app:layout_behavior="#string/appbar_scrolling_view_behavior" to include layout.
Set the layout_width and layout_height to match parent for the include layout(The layout that is inside the include layout).
you should set your theme to any NoActionBar theme such as
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorSecondary</item>
<item name="colorAccent">#color/google_blue</item>
</style>
When I first start looking over this question I found this question, but the answer didn't work for me. I am working on a KitKat phone, but from what I read, this can be done on a pre-lollipop versions using the AppCompat library. I am using this GitHub repo, and more specificaly this code sample to create nested preference screen.
Here is my AppTheme.SettingsTheme for the SettingActivity in the code sample:
<style name="AppTheme.SettingsTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="titleTextStyle">#android:color/white</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlNormal">#fff</item>
</style>
This line: <item name="colorControlNormal">#fff</item> changes the Toolbar from having a white text and black back arrow, to having both the text and back arrow appear white (which is good).
The issue is that I have a CheckBoxPreference which is tinted with this line also, so when it in not check, I can barely see it (box color is white).
I tried creating a special theme just for the toolbar (again from this answer) and doing the <item name="colorControlNormal">COLOR</item>, but its just doesn't affect to toolbar.
I also tried to tint the CheckBoxPreference alone, but it seems that in involves creating a custom layout for it, and this brings a lot more work.
Here is my toolbar layout, currently with no specific theme:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.activities.settingsActivity.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:minHeight="?actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
app:navigationIcon="?homeAsUpIndicator"
app:title="#string/activity_settings_title"
app:titleTextColor="#android:color/white" />
</android.support.design.widget.AppBarLayout>
Can anybody explain how to tint the CheckBoxPreference and Toolbar seperatly? I don't care which one of them I tint using the ActivityTheme and which one I tint on its custom theme. The answers I found didn't work.
Use the theme for your Toolbar specifically, instead of for your entire Activity. This is what the ThemeOverlay styles are meant for (they use the colorControlNormal attribute when necessary). You can also define a separate "popup" theme if you would like light-themed pop-ups from your dark Toolbar:
<style name="SettingsTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="SettingsTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="SettingsTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
Then you can apply these styles in your layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SettingsTheme.AppBarOverlay"
tools:context=".ui.activities.settingsActivity.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:minHeight="?actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
app:navigationIcon="?homeAsUpIndicator"
app:title="#string/activity_settings_title"
app:popupTheme="#style/SettingsTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
I want to change my toolbar text size and gravity. I create style in styles.xml like this:
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
<item name="android:color">#color/colorHighlight</item>
<item name="android:gravity">center</item>
</style>
and I set titleTextAppearance, everything here looks perfect but it didn't worked. My custom toolbar:
<?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:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#009688"
android:titleTextAppearance="#style/Toolbar.TitleText"
android:paddingTop="0dp"
app:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
What is wrong with my toolbar?
my toolbar
try using
app:titleTextAppearance="#style/Toolbar.TitleText"
instead of
android:titleTextAppearance="#style/Toolbar.TitleText"
as shown here
For centering gravity refer to this question and this question
I am trying as best I can to style the ActionBar (i.e. an android.support.v7.widget.Toolbar) in my app, but it just won't work. I am following the official documentation when trying to achieve this. What I am really trying to do is to make the title color white. It is currently displayed in black. My target API is 11.
What am I doing wrong?
res/values/themes.xml
<resources>
<!-- Base theme applied no matter what API -->
<style name="MMTheme.Base" parent="Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColor">#color/mm_dark_gray</item>
<item name="colorPrimary">#color/mm_brown</item>
<item name="colorPrimaryDark">#color/mm_dark_gray</item>
<item name="colorAccent">#color/mm_green</item>
<item name="android:actionBarStyle">#style/MMActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MMActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/mm_green</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MMActionBar</item>
<item name="actionBarTabTextStyle">#style/MMActionBarTabText</item>
<item name="actionMenuTextColor">#color/mm_green</item>
</style>
<style name="MMTheme" parent="MMTheme.Base"></style>
<!-- ActionBar style -->
<style name="MMActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#color/mm_green</item>
<item name="android:titleTextStyle">#style/MMActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/MMActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MMActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/mm_white</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<!-- ActionBar tabs text -->
<style name="MMActionBarTabText" parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#color/mm_green</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>
The inflated android.support.v7.widget.Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mm_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
In the Activity
// Setup main Toolbar
Toolbar toolbar = (Toolbar) mInflatedActivity.findViewById(R.id.mm_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
I have tried everything I can think of and searched a lot online, but I cannot make it work. What am I missing?
Current result is (nevermind the search icon):
Adding style="#style/MMActionBar" to the xml properties of the android.support.v7.widget.Toolbar results in this:
Changing main parent theme to Theme.AppCompat.Light.DarkActionBar does nothing.
Just use this in yout layout xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/red600"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Set theme in xml itself to ThemeOverlay.AppCompat.Dark.ActionBar which will make title text white.
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
And I guess this is the right way to theme Toolbar, since according to official Android Developer blog
Styling of Toolbar is done differently to the standard action bar, and
is set directly onto the view.
Here's a basic style you should be using when you're using a Toolbar
as your action bar:
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
The app:theme declaration will make sure that your text and items are
using solid colors (i.e 100% opacity white).
I have faced same problem. The following solution solved my problem.
For this you need to make changes at two places.
1.Modify your toolbar xml like this,
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/ColorPrimary"
android:elevation="2dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/TV_tool_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My App Name"
android:textSize="18sp"
android:textColor="#FFFFFF"/>
</android.support.v7.widget.Toolbar>
Modify like this in your activity file.
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
Thats it. This will make your title color white. Cheers..!
Use this to change colors of the letters on the toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp"
>
</android.support.v7.widget.Toolbar>
I am using the toolbar for android.
I just want to change the background color of the overflow menu. But it is not changing.
Style xml
<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="popupTheme">#style/PopupMenuStyle</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="PopupMenuStyle" parent="android:Widget.Holo.Light.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
</style>
Toolbar XML
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="2dp"
android:theme="#style/MyDarkToolbarStyle" />
To change the toolbar options menu color, add this to your toolbar element
app:popupTheme="#style/MyDarkToolbarStyle"
Then in your styles.xml define the popup menu style
<style name="MyDarkToolbarStyle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:colorBackground">#color/mtrl_white_100</item>
<item name="android:textColor">#color/mtrl_light_blue_900</item>
</style>
Note that you need to use colorBackground not background. The latter would be applied to everything (the menu itself and each menu item), the former applies only to the popup menu.
Edit:
if you just want a white overflow popup menu just do this
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="2dp"
app:theme="#style/MyDarkToolbarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"" />
and remove the redundant popupTheme your Style xml
<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
You should also include this in your top (parent) Layout
xmlns:app="http://schemas.android.com/apk/res-auto"
Simplest way
If you just want a white overflow popup menu just do this:
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ColorPrimary"
android:elevation="2dp"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Also, take a look at the value of android:layout_height attribute.