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>
Related
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>
I have:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorAccent">#color/myColor</item>
</style>
But I want to allow the user to change the accent color. Can I do it with AppCompat?
No you can't, because the accent color is defined in the theme and themes are read-only in Android.
The only thing you can do is switch themes or set the color of each component manually.
Note: you can apply a theme to a portion of UI instead of the whole Activity in order to change the accent color (or other things) locally. To do so, you can use the android:theme attribute in your XML layout with the AppCompat library, or you can inflate a layout by providing a ContextThemeWrapper as context to your LayoutInflater.
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.
In my application, I used Theme.Holo.Light to make the ActionBar look light and I am pretty pleased with the result.
The only problem is that all my widgets (EditText, Spinner, etc..) are acting like the background was white, while I set it a dark blue:
Is there an easy way in the style.xml file where I can force everything to act like the theme was dark?
I know that I could theme the text color, background resources, etc.. But this would be a huge work.
Thank a lot for any hint that could help me to achieve that easily.
Edit: what sounds the best solution, but not working:
<style name="AppBaseTheme" parent="#android:Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
</style>
To solve this (the easy way) you should use Theme.Holo. And Change the actionbar to Theme.Holo.Light.
extend theme:
<style name="MyTheme.Dark" parent="android:Theme.Holo.Light.ActionBar"></style>
use getActionBar().getThemedContext() as the context for inflating your views.
You can use Holoeverywhere library to achieve your result.
HoloEveryWhere
I've got a custom theme that inherits from Theme.Holo.Light.DarkActionBar and I'm trying to change the theme's tabWidget style so it to look like if the theme was Theme.Holo Dark.
I've tried this in my theme with no result
<style name="AppTheme" parent="android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:tabWidgetStyle">#android:style/Widget.Holo.TabWidget</item>
...
</style>
The only thing I can appreciate is there is a small change in the blue bar of the tab.
Any idea how to get it? Thanks!
Try also the items "android:tabStripLeft" and "android:tabStripRight".