Android actionbar color doesnt change - android

I have the following styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
</style>
</resources>
Where primary is defined as:
<color name="primary">#9c27b0</color>
However in the preview the actionbar is still black and not deep purple.
When i open the theme editor the actionbar is also deep purple.
Is this a bug in the preview? Or have i done something wrong?
In general how exactly do i use the styles.xml? Can i also define button's in it?

After editing the color code in the color.xml file, you should rebuild it. Try rewriting the code and then rebuilding. I faced the same problem, that's ho

Related

How to change style of PreferenceThemeOverlay / PreferenceFragmentCompat

I worked myself thru the AndroidFundamtentals Tutorial 09.2 (App Settings) but I couldn't find a working solution for my problem.
I want to change the style (backgroundcolor) of the Settings-Fragment in my app.
That's what I have coded in the styles.xml so far:
<style name="AppThemeWithActionBar" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="colorPrimary">#color/cr_blue_dark2</item>
<item name="colorPrimaryDark">#color/cr_blue_dark</item>
<item name="colorAccent">#color/cr_green</item>
<!-- more styles-->
<item name="preferenceTheme">#style/AppTheme.SettingsScreen</item>
</style>
<style name="AppTheme.SettingsScreen" parent="PreferenceThemeOverlay">
<item name="backgroundColor">#color/cr_black</item>
<item name="background">#color/cr_black</item> <!--one of these should make the background black-->
</style>
This code does not change the background color of the Settings fragment.
If you need more code just write an comment. :)
I just found the solution / mistake.
The parent of the style AppThemeWithActionBar is wrong.
It changes the background to light.
Change the title of the style like this:
<style name="AppThemeWithActionBar" parent="Theme.MaterialComponents">
Also change the theme of the activity in the Manifest.xml file.

ActionBar background with MaterialComponents theme

I want to customize my ActionBar. My Theme looks as below:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/ThemeOverlay.MaterialComponents.ActionBar">
<item name="background">#drawable/actionBarBackground</item>
</style>
In values folder:
<drawable name="actionBarBackground">#FFFFFFFF</drawable>
In values-night folder:
<drawable name="actionBarBackground">#FF000000</drawable>
Not sure why my ActionBar background colour doesn't change accordingly. I have tried to change my theme in different other ways as well but nothing works. Here are my other tries:
Instead of actionBarStyle, I used actionBarTheme.
<item name="actionBarTheme">#style/MyActionBar</item>
I also tried using colorPrimary.
<item name="colorPrimary">#color/actionBarBackground</item>
Am I missing something?
Since you are using a Theme.MaterialComponents.DayNight app theme the ActionBar background color is defined by default by the colorPrimary attribute.
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/mycolor</item>
</style>
where mycolor is define in res/values/colors.xml
<resources>
<color name="mycolor">#xxxxxx</color>
...
</resources>
You can also customize the background color using the actionBarStyle attribute in your app theme.
Something like:
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">#style/Custom.ActionBar</item>
...
</style>
where:
<style name="Custom.ActionBar" parent="Widget.MaterialComponents.Light.ActionBar.Solid">
<item name="background">#color/mycolor</item>
</style>
Otherwise you can use the actionBarTheme attribute to override the background color:
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarTheme">#style/ThemeOverlay.ActionBar</item>
...
</style>
<style name="ThemeOverlay.ActionBar" parent="">
<item name="colorPrimary">#color/mycolor</item>
</style>
For me neither setting background nor colorPrimary has helped.
Apparently this is the intended behavior, as according to dark theme guidance for components that use a large portion of a screen. They should use colorSurface for their background instead of colorPrimary or colorAccent. AppBarLayouts, Toolbars, and ActionBars now default to their Surface styles in dark theme.
So I had to define colorSurface attribute for my theme in order for it to work.
<item name="colorSurface">#color/my_color_primary_dark</item>
I'm on the latest stable of MDC, and this is how it works here.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/purple_500</item>
<item name="actionBarStyle">#style/Widget.MaterialComponents.ActionBar.PrimarySurface</item>
</style>
If you want the app bar to be the same color as your color surface, change #style/Widget.MaterialComponents.ActionBar.PrimarySurface to #style/Widget.MaterialComponents.ActionBar.Surface
This is most likely because that's not the right way to declare colours in an app.
Typically, colour resources are located in one-singular file: colors.xml
This XML file is typically located in your app's resources folder (usually res/values/colors.xml)
They are then declared with a <resources> element as the root of the XML file and your colour resources (as defined with the <color> element):
<!-- Note: The XML header (aka <?xml version="1.0" ...?>) is optional -->
<resources>
<color name="your_color_res">#FFFFFF</color>
<!-- Your other colour resources go here -->
</resources>
You can then specify qualifiers for your colour resources by creating the same colors.xml in your app's resources with the following file format:
res/values-<qualifier>/colors.xml
These colours can then be referenced with the #color/ prefix in XML files or with R.color.* in your app's Java/Kotlin code.
Hope this helps!

in android studio i am using AppCompat api28 , how can i change toolbar attributes like background color and etc in style.xml file?

I have tried many options but whatever I use, it seems I cannot change the toolbar or action bar color & styles in the 'style.xml' file. it seems like using 'android.support.v7.widget.toolbar' is my only option . do you know a way so that I can change action bar styles in 'style.xml' file?
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/ThemeOverlay.AppCompat.ActionBar">
<item name="android:background">#ff0000</item>
</style>
and in the manifest i used 'CustomActionBarTheme' as my default theme .
i found the solution , my first mistake was that i should use a theme like :
'Theme.AppCompat.Light' , anything that starts with Theme , and finally creating an style like bellow , with parent set to 'Widget.AppCompat.ActionBar'
<style name="customActionbarStyle" parent="Widget.AppCompat.ActionBar">
<item name="background">#color/your_custom_color</item>
// also notice you must use color.xml file for colors and for some attributes
// like titleTextStyle , you must define another style and use it here like :
<item name="titleTextStyle">#style/your_title_text_style</item>
</style>
<style name="your_title_text_style">
<item name="android:textColor">#color/your_text_color</item>
</style>
you can find all other properties of ActionBar bellow :
https://developer.android.com/reference/android/R.styleable.html#ActionBar

Status Bar color not appied to api-21

Hi all I want to change the status bar color, But Mu status bar color is showing black. I tried to search for this I got couple of question about this on stackOverflow but it does not help me. Still on api-21 it shows me black color.
Following is my color.xml inside values-v21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primaryColor">#3F51B5</color>
<color name="primaryColorDark">#FFA000</color>
<color name="accentColor">#F44336</color>
my style.xml inside values-v21
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:colorAccent">#color/accentColor</item>
</style>
and my style.xml is like:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
As one answer suggest therefore I also tried to add following line before setContentView(R.layout.main) as
getWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
but here i did got error in this line as getWindows can not resolve symbol
Hey i guess you missed something typo error 'getWindow' to 'getWindow()'
:-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
As a sanity check, are you testing on lollipop? even if your using SDK 21 and AppCompat, it will only show on lollipop devices.
Assuming you are testing on lollipop, the next thing I would check is that the theme is being applied to your activity (or set as the default theme) in your AndroidManifest.xml

How to change colour of task bar

I want change the colour of the task bar i.e., The color of the very top of the line where the time and battery display etc. are located suppose to be dark gray instead of black. I have used
setTitleColor(color.darker_gray);
But, It did not get effect. can anyone helpĀ me.
Thanks in advance.
your can use this library :) it works only for kitkat + :)
update: the library name is : SystemBarTint in github.com
By Changing Theme
<style name="AppTheme" parent="android:Theme.Light">
....
<!--Changing windowBackground will let your status change the color in most of the device-->
<item name="android:windowBackground">YOUR_COLOR</item>
...
</style>
By Code
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
You can define your custom theme for the app and set the action bar background there
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
Then in your app manifest file set the theme to MyTheme

Categories

Resources