I am Struggling to change the color of the status bar for each activity.
I have tried the create theme and style listed in posts on this site to no avail.
How can I make each activity have a status bar of different color?
You can change it by setting the android:statusBarColor or android:colorPrimaryDark attribute of the style you're using for your app in styles.xml.
(android:statusBarColor inherits the value of android:colorPrimaryDark by default)
For example (since we're using an AppCompat theme here, the android namespace is omitted):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#color/your_custom_color</item>
</style>
On API level 21+ you can also use the Window.setStatusBarColor() method from code.
Related
I have received a design spec. in which the status bar icons' color is to be changed completely. I know I can change the status bar color in theme, however that is not my question. I want to override the icon color/icons themselves as shown in the image attached.
I searched a bit but could not find a way to do it.
I am aware that as a last resort, I can change the existing status bar icon tintcolor the following attribute in my app theme.
<item name="android:windowLightStatusBar" tools:targetApi="23">true</item>
You can change it by setting the android:statusBarColor or android:colorPrimaryDark attribute of the style you're using for your app in styles.xml.
(android:statusBarColor inherits the value of android:colorPrimaryDark by default)
For example (since we're using an AppCompat theme here, the android namespace is omitted):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#color/your_custom_color</item>
</style>
On API level 21+ you can also use the Window.setStatusBarColor() method from code.
<item name="colorPrimaryDark">#color/your_color</item>
will only be visible in Lollipop and greater than Lollipop(API) devices. P.S. you need to have Theme.AppCompat as your base/main theme
I'm trying to make the background colour of my ActionBar a colour corresponding to the code. For some reason my app crashes when it reaches this line of code.
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845")));
When I remove it the error goes (ActionBar background is set to black by default). I've tried rearranging the line to be above and below
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_register1);
but still no luck.
This is the theme I'm using if it has any relevance
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> </style>
Thanks in advance for any help
If you are using AppCompat, you need to call getSupportActionBar() instead.
If you want to change the color by style with the recent versions of AppCompat, you just need to set the "colorPrimary" attribute in your theme to the color you want.
you can set it in the style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_action_bar_color</item>
</style>
Theme.Holo is used in the tutorial http://developer.android.com/training/basics/actionbar/setting-up.html
I'm using Android Studio, and the Theme.AppCompat.Light.DarkActionBar is the default style used in the styles.xml
The emulator gives an error Unfortunately, Action Bar has stopped if I comment out AppCompat theme in styles.xml and add style name="AppTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar
How can Android Studio be setup so that Holo is the default theme, or how can the theme be changed to match the tutorial?
Change the default code in styles.xml to:
<style name="AppTheme" parent="android:Theme.Holo.Light">
That does it right? Let me know.
I have an Activity with an action bar. minSdkVersion is 11.
The action bar is a grey color, I would like to change it to a different color so that it matches other colors in my application.
I have created the following styles
<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">#FFF</item>
</style>
And set this activity's theme in the appmanifest to MyTheme but when I run the application I get this error:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
What am I doing wrong?
See https://developer.android.com/training/basics/actionbar/styling.html, specifically these parts
Note: If you are using the Support Library APIs for the action bar,
then you must use (or override) the Theme.AppCompat family of styles
(rather than the Theme.Holo family, available in API level 11 and
higher)
and
When using the Support Library, you must instead use the Theme.AppCompat themes:
Theme.AppCompat for the "dark" theme.
Theme.AppCompat.Light for the "light" theme.
Theme.AppCompat.Light.DarkActionBar for the light theme
with a dark action bar.
Basically you have to replace 'Holo' with 'AppCompat'
If you are using ActionBarActivity, you need to use an AppCompat theme, which brings the Lollipop style Action Bars to all devices and support for the Material Color Palette allowing you to write a theme such as
<style name="MyTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="ColorPrimary">#color/primary</item>
</style>
to style your action bar color automatically.
If you'd prefer to not use any of that (and look very different on Lollipop devices), you can instead extend FragmentActivity or Activity, depending on if you want to use the support library fragments (which backport and fix a number of issues around nested fragments and state saving).
Just use this, if your extending ActivityActionBar on your activity then you need to use the AppCompat themes.
<style name="AppCompatTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="ColorPrimary">#color/primary</item>
</style>
or use style/Theme.AppCompat.Dark for white text :)
There are the following Theme.AppCompat classes:
Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar
Theme.AppCompat.NoActionBar
Theme.AppCompat.Light.NoActionBar
Theme.AppCompat.DialogWhenLarge
Theme.AppCompat.Light.DialogWhenLarge
Theme.AppCompat.Dialog
Theme.AppCompat.Light.Dialog
Theme.AppCompat.CompactMenu
and the following ThemeOverlay.AppCompat classes:
ThemeOverlay.AppCompat
ThemeOverlay.AppCompat.Light
ThemeOverlay.AppCompat.Dark
ThemeOverlay.AppCompat.ActionBar
ThemeOverlay.AppCompat.Dark.ActionBar
Why would one use ThemeOverlay.AppCompat.light vs Theme.AppCompat.Light for example? I see that there are much less attributes defined for ThemeOverlay -- I am curious what the intended use case for ThemeOverlay is.
Theme.AppCompat is used to set the global theme for the entire app. ThemeOverlay.AppCompat is used to override (or "overlay") that theme for specific views, especially the Toolbar.
Let's look at an example for why this is necessary.
App themes with an ActionBar
The ActionBar is normally shown in an app. I can choose it's color by setting the colorPrimary value. However, changing the theme changes the color of the text on the ActionBar.
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Since my primary color is dark blue, I should probably use one of the themes that uses a light text color in the action bar because the black text is hard to read.
Hiding the ActionBar and using a Toolbar
The whole point of using Theme.AppCompat rather than Theme.Material is so that we can allow older versions of Android to use our material design theme. The problem is that older versions of Android don't support the ActionBar. Thus, the documentation recommends hiding the ActionBar and adding a Toolbar to your layout. To hide the ActionBar we have to use one of the NoActionBar themes. The following images show the Toolbar with the ActionBar hidden.
But what if I want something like a Light theme with a DarkActionBar? Since I have to use NoActionBar, that isn't an option.
Overriding the App Theme
Here is where ThemeOverlay comes in. I can specify the Dark ActionBar theme in my Toolbar xml layout.
<android.support.v7.widget.Toolbar
...
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
This finally allows us to have the effect we want. The Dark.ActionBar theme overlays the Light app theme for this particular occasion.
App Theme: Theme.AppCompat.Light.NoActionBar
Toolbar Theme: ThemeOverlay.AppCompat.Dark.ActionBar
If you wanted the popup menu to be light you could add this:
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
Further Study
I learned this through experimentation and through reading the following articles.
Setting Up the App Bar
Use android:theme and ThemeOverlay to theme specific Views and their descendents
Theming with AppCompat
Use colorPrimary to colorize your App Bar
Theme vs Style
Per this Theme vs Style blog post by the creator of AppCompat:
[ThemeOverlays] are special themes which overlay the normal Theme.Material themes, overwriting relevant attributes to make them either light/dark.
ThemeOverlay + ActionBar
The keen eyed of you will also have seen the ActionBar ThemeOverlay derivatives:
ThemeOverlay.Material.Light.ActionBar
ThemeOverlay.Material.Dark.ActionBar
These should only be used with the Action Bar via the new actionBarTheme attribute, or directly set on your Toolbar.
The only things these currently do differently to their parents is that they change the colorControlNormal to be android:textColorPrimary, thus making any text and icons opaque.