I'm trying to change the background and title color of the toolbar on Android 10.
In the xml file, I have the following (in style):
<item name="colorPrimary">#color/C_Yellow</item>
<item name="toolbarStyle">#style/CustomStyle</item>
(in resources):
<style name="CustomStyle" parent="Widget.AppCompat.Toolbar">
<item name="titleTextColor">#color/B_Blue</item>
</style>
It works on Android 4.4; however, on Android 10, it just has a black background with white text.
What should I do in order to change both background and title color on Android 10?
Just use android:foregroundTint for text color and icons if present, secondly for background use android:background and set color according to your choice
Related
The new 'Material Design 3 top app bar' docs says they got rid of the drop shadow.
How can I enable it? Setting elevation on Toolbar or AppBar does not work at all.
I had the same situation. I found that:
The shadow drop applies starting from API 28, below API 28 - the shadow effect is the same as with a MaterialComponents theme.
A color fill works below API 28 (tested on API 26).
Docs for Top app bar specs says that the container of the TopAppBar has a role "Surface" and Elevation (on scroll) Level 2.
On the page Color system - Color roles I found information that:
At elevation +2 components with surface color containers receive a primary color overlay with 8% opacity.
So the default style for a TopAppBarLayout uses ?attr/colorSurface as a background color and ?attr/colorPrimary with 8% opacity as an overlay (kind of a scrim effect).
My solution:
Case 1 - Only enable a shadow effect.
Create a style for AppBarLayout and set android:outlineAmbientShadowColor and android:outlineSpotShadowColor to black (as it's a default color for creating shadow). These attributes are set as transparent in Widget.Material3.AppBarLayout.
<style name="Widget.App.AppBarLayout" parent="Widget.Material3.AppBarLayout">
<item name="android:outlineAmbientShadowColor" ns1:ignore="NewApi">#android:color/black</item>
<item name="android:outlineSpotShadowColor" ns1:ignore="NewApi">#android:color/black</item>
</style>
Case 2 - Enable a shadow effect and get rid of the overlay.
In addition to the above you can add either an android:background attribute with you color or a materialThemeOverlay attribute with setting colorSurface to your color (a background) and colorPrimary to #android:transparent (an overlay). I prefer to add directly android:background because adding materialThemeOverlay can have impact on the child views of your AppBarLayout which.
<style name="Widget.App.AppBarLayout" parent="Widget.Material3.AppBarLayout">
<item name="android:outlineAmbientShadowColor" ns1:ignore="NewApi">#android:color/black</item>
<item name="android:outlineSpotShadowColor" ns1:ignore="NewApi">#android:color/black</item>
<item name="android:background">#color/white</item>
</style>
or
<style name="Widget.App.AppBarLayout" parent="Widget.Material3.AppBarLayout">
<item name="android:outlineAmbientShadowColor" ns1:ignore="NewApi">#android:color/black</item>
<item name="android:outlineSpotShadowColor" ns1:ignore="NewApi">#android:color/black</item>
<item name="materialThemeOverlay">#style/ThemeOverlay.App.DayNight.NoActionBar</item>
</style>
<style name="ThemeOverlay.App.DayNight.NoActionBar" parent="Theme.Material3.DayNight.NoActionBar">
<item name="colorPrimary">#android:color/transparent</item>
<item name="colorSurface">#android:color/white</item>
</style>
Don't forget apply your style to your AppBarLayout or theme.
By the way, a liftOnScroll attribute is set to true in Widget.Material3.AppBarLayout so there's no need for setting it. Everything works with setting only layout_behavior for a scrolling view.
fHow can I change the color of the app name, I have changed the toolbar text color to white but the app name is still in black. I have also noticed if I change the color of the bar to black the app name color becomes white. I want the app name color to be like WhatsApp.
try this
<style name="MyActionBarTitleText"
parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/darker_gray</item>
</style>
I hope it will help you .
You can try Custom Toolbar with Custom Color Values..
For my android project I have some default buttons.
To make the app look nicer I try to change the colors with the following code :
<style name="CustomStyle"parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:buttonStyle">#style/CustomButton</item>
</style>
<style name="CustomButton" parent="android:style/Widget.Button">
<item name="android:background">#color/buttonBackgroundColor</item>
<item name="android:textColor">#color/buttonTextColor</item>
</style>
The color of the buttons are adjusted like I want but all the exisiting default heights and paddings are gone.
How should I adjust the colors of my button without losing the existing padding and height and other default values?
I want to change the style for all buttons in my app in multiple fragments/activitys so doing it programmaticly is not prefered.
Minimum api level that I use is 14, if I used api level 21 or higher I could use backgroundTint instead of background and get the right result but this is not an option now.
Android default button uses a 9 patch drawable as background.
which has some default padding, look at the content area defined by the right and bottom lines.
You can define padding as well in the custom style you have.
<style name="CustomButton" parent="android:style/Widget.Button">
<item name="android:background">#color/buttonBackgroundColor</item>
<item name="android:textColor">#color/buttonTextColor</item>
<item name="android:padding">#dimen/custom_button_padding</item>
</style>
You can do it programatically with :
yourButton.setBackgroundColor(getResources().getColor(Color.parseColor("#FFFFFF")));
or if you want to do it using styles:
1) You should insert a dimension value within the style xml:
<dimen name="buttonHeight">40px</dimen>
2) Reference the dimension value. So in your layout file you'll write:
android:layout_height="#dimen/buttonHeight"
Surfing the web I've found this site which generates Android custom themes. I've used it to create my personal theme: it inherits from Android:Widget.Holo (should be the dark one with white text) and some fancy widget witha a custom color.
Because the theme inherits from Android:Widget.Holo, I have dark background and white text; but in some activity i need the opposite: white background with dark text.
When it comes to change the text color of most item i can do it easily using android:textColor="#android:color/black", but when i try to use it on spinner item it does nothing.
I've tried to create a new style only for spinner items, but i canno't apply it...
This is the first style generated (white text on black background):
<style name="SpinnerAppTheme" parent="android:Widget.Holo.Spinner">
<item name="android:background">#drawable/apptheme_spinner_background_holo_dark</item>
<item name="android:dropDownSelector">#drawable/apptheme_list_selector_holo_dark</item>
</style>
And this is the second I've generated (the opposite, back text on white background):
<style name="SpinnerDarkTextAppTheme" parent="android:Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/apptheme_spinner_background_holo_light</item>
<item name="android:dropDownSelector">#drawable/apptheme_list_selector_holo_light</item>
<item name="android:textColor">#android:color/black</item>
</style>
But when I use style="#style/SpinnerDarkTextAppTheme nothing happens.
How can I do to have 2 different styles (obviously working at the same time) for my spinners?
SOLVED using this piece of code.
You can create two themes in the XML and switch between them before you set the content view:
https://stackoverflow.com/a/6390025/2219600
Another approach:
https://stackoverflow.com/a/16101936/2219600
I am trying to set the background color for all my entire app and I could not do it. WHat I am doing is this:
<style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowBackground">#color/holo_gray</item>
<item name="android:colorBackground">#color/holo_gray</item>
</style>
I also tried with:
<item name="android:background">#color/holo_gray</item>
I want to have the same default background color of Api 14 in lower APIs, that light gray.
I know that I could fix it by adding a android:background="#color/holo_gray" in every root layout but I want to do it by style, to avoid repeat this line in all my xml layout.
Thanks in advance,
holo_gray is not work in lower version so you should write you own color instead of it.Making the change in all value folder styles
#color/"holo_gray"(Replace your own color)
If you have ListViews in your layouts, they possess a default white background on 2.2. Try setting android:background="#null" on them, so your window background can show up.