When should one use Theme.AppCompat vs ThemeOverlay.AppCompat? - android

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.

Related

color of status bar in Android Studio

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.

Changing status bar icon color

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

Toolbar theme not applied from styles (22.1.0)

Prior to appcompat version 22.1.0, I was able to define a global style for my apps toolbars in styles.xml.
<item name="toolbarStyle">#style/AppTheme.Widget.Toolbar</item>
My global toolbar style declared a theme attribute for the toolbar:
<style name="AppTheme.Widget.Toolbar" parent="Widget.AppCompat.Toolbar">
<item name="theme">#style/AppTheme.Widget.Toolbar.ThemeOverlay</item>
</style>
After upgrading to 22.1.0 and changing the theme attribute to the new android:theme attribute, the theme no longer gets applied. If I declare this theme on a toolbar in a layout.xml, it works.
How can I declare a global theme for toolbars with a global style?
It's not a bug, as android:theme has greater meaning over any style (styles get their values from themes).
This did work in previous versions as a side effect of the implementation. The implementation is now much closer to how LayoutInflater works in Android 5.0+.

Change background color of Android Action Bar

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 :)

keeping current theme and adding actionBar to app

I'm currently using the standard Theme.Light.NoTitleBar and would like to keep that, but to use an actionBar I have to use Theme.Sherlock as the parent. Is there a way around this?
If not, how can I use Theme.Sherlock as a parent and keep what I had before?
If you use Theme.Light.NoTitleBar then ActionBar will not be shown. ActionBar is nothing but a type of TitleBar.
If you already have some theme with some styles and want to use the actionbar then try below
For Dark theme
<style name="YourTheme" parent="Theme.Sherlock"></style>
For Light theme
<style name="YourTheme" parent="Theme.Sherlock.Light"></style>

Categories

Resources