Android custom toolbar theme not being applied - android

I have an app that uses a custom Toolbar style. Previously, it applied the theme correctly, but after a recent update of Android Studio, it has stopped working correctly.
The theme.xml is defined like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="OurTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
<item name="android:listSeparatorTextViewStyle">#style/BlueListSeparatorTextViewStyle</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/action_bar_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<!--<item name="colorPrimaryDark">#color/my_awesome_darker_color</item>-->
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/btn_accent_color</item>
</style>
<!-- Application theme. -->
<style name="OurTheme" parent="OurTheme.Base">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:listSeparatorTextViewStyle">#style/BlueListSeparatorTextViewStyle</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
</resources>
Our styles.xml contains:
<!-- ActionBar styles ThemeOverlay.AppCompat.ActionBar -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
</style>
actionbar_text is defined in our colors.xml:
<color name="actionbar_text">#ffffffff</color>
Finally, our custom toolbar layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark" />
Previously, this worked and our app had white text in its custom toolbar. It now defaults to black everywhere in the app and the only method I have found that can change the color of the toolbar title text (other than doing it in code) is to change the toolbar layout to this:
<?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="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextAppearance="#style/MyActionBarTitleText"
app:theme="#style/ThemeOverlay.AppCompat.Dark" />
Note that now I am specifically telling the actionbar to use MyActionBarTitleText style.
I have also tried to specifically tell it to use the custom theme here as well, by changing the "app:theme" to:
app:theme="#style/MyActionBar"
but this hasn't helped.
I'm afraid I'm at a loss. I know the base theme works as I can change the background colour, but using the actionBarStyle item simply refuses to work.
This has also affected text layouts and justifications to some of our custom button layouts as well and I can't figure out why it has stopped working. Does anyone have any suggestions?

It's very late but answering it.
Create a custom theme for your toolbar and set android:background
<style name="ToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:background">#color/blue</item> // change your color here and add other attributes
</style>
Now apply this theme to your toolbar in XML like:
style="#style/ToolbarStyle"

Related

How to change Menu indicator color in activity ToolBar

I want to change menu indicator color in activity ToolBar, Now its color is not white or it has alpha option, I want to be White
I am using AppCompatActivity and here is my toolbar xml code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/primary"
android:elevation="2dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" />
Thanks
You can custom overflow icon with white color and apply that icon in your theme style like this
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/CustomOverFlow</item>
</style>
<style name="CustomOverFlow" parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">#drawable/your_custom_icon</item>
</style>
You just have to change the colorControlNormal attribute.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorControlNormal">#FFFFFF</item>
</style>

Android: StatusBar White after setting fitsSystemWindows and toolbar having a primaryDarkColor

I have been debugging this for quite sometime and cannot seem to find the solution. My problem is that my statusbar is white even when I have my view with the xml line
android:fitsSystemWindows="true"
A picture of the problem can be found below. Anyway, I have tried multiple solutions, but none have worked. Here is the code for my toolbar.
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="3dp"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/AppTheme.ActionBarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/actionbar_color"/>
</android.support.design.widget.AppBarLayout>
Here is the code for AppTheme.ActionBarStyle
<style name="AppTheme.ActionBarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:background">#color/actionbar_color</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Light</item>
</style>
Here is a picture of the problem. What am I missing here? Also note that I did set the colors in the apptheme
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/accent</item>
</style>
If your phone is pre-Lollipop then you will not be able to achieve it like this.Code you have written is correct and will show correctly on Lollipop devices.
Adding these style settings to your AppTheme may work:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
Note that this can work only for API-21 (Lollipop) or later.
This is a bit embarrassing, but I was using a different style in my Manifest file. That is why it wasn't working.

ActionMode with AppCompat can't change background and text colors

I'm trying to change the background and text colors for my ActionMode. I'm using API 11 and AppCompat.
I've tried it with the following style.xml, but the ActionMode background is always white and the text color is black.
I want to change text color to white and background color to blue.
How can I solve this?
Here is my style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ActionMode -->
<item name="actionModeStyle">#style/MyActionModeStyle</item>
</style>
<!-- Setup the style of the ActionMode here -->
<style name="MyActionModeStyle" parent="#style/Widget.AppCompat.ActionMode">
<item name="android:background">#color/blue</item>
<item name="background">#color/blue</item>
<item name="android:titleTextStyle">#style/MyActionModeTitle</item>
<item name="titleTextStyle">#style/MyActionModeTitle</item>
</style>
<!-- Setup the text style of the ActionMode here -->
<style name="MyActionModeTitle" parent="#style/TextAppearance.AppCompat.Widget.ActionMode.Title">
<item name="android:textColor">#ffffff</item>
</style>
</resources>
In my Activity I'm using android.support.v7.app.ActionBarActivity but I use toolbar:
Here is my settings_activity_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"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="#string/action_settings" />
Try to set android:actionModeStyle too:
<item name="android:actionModeStyle">#style/MyActionModeStyle</item>
Ok, I managed it by replacing the HomeAsUpIndicator programatically with an done button. Thats all I wanted...

Define toolbar gradient in styles.xml

I am trying to define a gradient background for a toolbar, but I am getting a weird effect where the gradient becomes the background of all the elements inside the toolbar:
Other answers suggest to set the background at layout but I am interested in getting it working at style level. Ay suggestion? Thanks in advance!
My defined styles are the following:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<!-- This puts the status bar translucent -->
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
<!-- set actionBar style. This att is referenced from layout to set the style-->
<item name="android:actionBarStyle">#style/AppThemeToolBar</item>
</style>
<style name="AppThemeToolBar" parent="#style/Theme.AppCompat">
<item name="android:icon">#drawable/ic_logo</item>
<item name="android:background">#drawable/toolbar_background</item>
<!-- trying to set a text without background but is being ignored -->
<item name="android:titleTextStyle">#style/ToolBarTitle</item>
<!-- also ignored -->
<item name="android:titleTextAppearance">#style/ToolBarTitle</style>
</style>
And this is the toolbar declaration at my layout xml:
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:layout_gravity="top"
<!-- here actionBarStyle att is read -->
app:theme="?attr/actionBarStyle"
/>
Solution is as simple/stupid as using toolbarStyle att at styles.xml:
<style name="MyTheme">
<item name="toolbarStyle">#style/NeoToolbarStyle</item>
<item name="toolbarNavigationButtonStyle">#style/ToolbarNavigationButtonStyle</item>
</style>
And of course my Toolbar style can be used as usual with android:background without getting the weird effect:
<style name="AppThemeToolBar" parent="#style/Widget.AppCompat.Toolbar">
<item name="android:background">#drawable/my_beautiful_gradient</item>
</style>

How to set the text color of action bar title using style

Hello I am trying to set the the ActionBar title color to White. I tried below code but that is not working. Currently title of it showing in black color. I don't know what I am missing. I tried diferent many attempts but didn't get success, other properties are working fine like background color etc.
I am using AppCompat library for backport.
Can anyone help me please.
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_dark_color</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- ActionBar Text Style -->
<item name="titleTextStyle">#style/titleStyle</item>
</style>
<style name="ActionBarCompat" parent="Widget.AppCompat.ActionBar">
<item name="atitleTextStyle">#style/titleStyle</item>
</style>
<style name="titleStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/action_bar_text_color</item>
</style>
</resources>
if you're using a Toolbar you can use the following code:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/it.onecalculator"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:titleTextAppearance="#style/MyActionBarTitleText" />
where MyActionBarTitleText is:
<style name="MyActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>

Categories

Resources