I am using the support v7 library to implement ActionBar in my app..I have this in my styles.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBarTheme</item>
</style>
<style name="ActionBarTheme" parent="android:Widget.ActionBar">
<item name="android:background">#FFFF0000</item>
</style>
</resources>
However, Eclipse complains in the actionBarStyle line. The error is this one:
android:actionBarStyle requires API level 11 (current min is 8)
What can I do to apply my theme to API levels 8-10?
You need to provide two API specific styles.xml. In your values/styles.xml use
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBarTheme</item>
</style>
and in your values-v14/styles.xml use
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBarTheme</item>
</style>
If you use latest v7 support library (v21 at the time of this post), there is no need to add android: prefix to any action bar attributes anymore.
In your case, adding the following to values/styles.xml will be sufficient:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBarTheme</item>
</style>
Reference: https://chris.banes.me/2014/10/17/appcompat-v21/#migration-from-previous-setup
Related
I want to use the AppTheme android:Theme.Material.Light.DarkActionBar for my app. This is available from SDK 21...My minimum SDK is 16. I have the support library com.android.support:appcompat-v7:22.0.0 in my build gradle file. I want to create a second styles file under the the values folder. I want to have two styles files...one for Lollipop...greyed out version 22 next to it...and one for pre Lollipop. How to do this?
As said create a values folder for a specific Api Level
res/values-v21/styles.xml
res/values/styles.xml
In values/styles.xml create to styles:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style
<style name="AppTheme" parent="AppTheme.Base">
</style>
In values-v21/styles.xml create to styles:
<!-- Themes for Android API21 -->
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:colorAccent">#color/accentColor</item>
</style>
Inside your AndroidManifest.xml you should use AppTheme as your global application theme
Further you should use the Toolbar from the SupportLibary instead of the default Actionbar. This is why the Base Theme sets the Actionbar to false
As suggested in the other answers you can use different styles.xml file in different folders.
I would like to add an option to other answers.
You can define a Base Style used by each versions, and customize singles attributes.
In res/values/styles.xml
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
<style name="AppTheme" parent="AppTheme.Base">
<!-- custom pre-Lollipop attributes -->
</style>
In res/values-v21/styles.xml
<style name="AppTheme" parent="AppTheme.Base">
<!-- custom Lollipop attributes -->
</style>
Finally use the AppTheme in your Activity (you have to use a ` ActionBarActivity).
You should add another folder called "values-v21" in your "res" directory. Then create a "styles.xml" within that directory for your Lollipop styles.
Create two styles.xml files in different folders, e.g:
res/values-v21/styles.xml
res/values/styles.xml
Hi I am building small android application in which I tried to change color for action-bar but it is not working for me. I tried it in following way:
// in values/themes.xml.
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:colorPrimary">#0097a7</item>
<item name="android:colorPrimaryDark">#0097a7</item>
<item name="android:colorAccent">#0097a7</item>
<!-- <item name="colorPrimary">#color/primary</item> -->
</style>
I am using appcompat_7.But above styling is not working for me. Am I doing something wrong. Need some help.
With the appcompat v21, you have to remove the android: attribute.
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#0097a7</item>
<item name="colorPrimaryDark">#0097a7</item>
<item name="colorAccent">#0097a7</item>
</style>
To use these styles you have to compile your project with API 21.
I have been trying to change the background colour of my ActionBar in my app. But it seems to give me some error.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
The error I get is on the
<item name="background">#drawable/actionbar_background</item>
I just want to change the background of the actionBar to a different colour.
Help will be appreciated. Thanks
If you target for minimum API level 11 , you can change ActionBar's background color by defining custom style, as:
<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>
And, set "MyTheme" as theme for application / activity.
You can also use this online tool to generate a full customized ActionBar
Here is tool
It's also possible to change the color of the ActionBar programmatically, just call this line:
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.my_color)));
That's also possible via getSupportActionBar() (if you're using ActionBarSherlock).
i have to customize my whole application theme and i am trying to implement my custom theme.
i am able to implement custom theme but it is showing error when i apply it in manifest file.
here is my code for custome theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
</style>
</resources>
You should provide more details about the error you are getting. At first sight I can see you are using Theme.Holo.Light.DarkActionBar, which I believe is only available for Android 4.x, so if you are targeting a previous version of Android you could get an error.
Try adding a style.xml file to each of the following folders:
res/values
res/values-v11
res/values-v14
Replace the referenced theme with Theme.Light in the style.xml file of the first folder. Use Theme.Holo.Light for v11 and leave Theme.Holo.Light.DarkActionBar in the style.xml file under v14.
Also bear in mind you cannot style the action bar under versions prior to Honeycomb (v11). Check the documentation for the ActionBar to get more details, in particular the information related to the AppCompat library.
I hope that helps. Otherwise, please provide more information about the error you are getting.
Change to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
</style>
</resources>
In manifest apply theme as
android:theme="#style/CustomActionBarTheme" >
Your min sdk in manifest should be 14. Use appcompact from the support library if you wish to support actionbar below api level 11.
Also check this
http://developer.android.com/guide/topics/ui/actionbar.html
https://developer.android.com/training/basics/actionbar/styling.html
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
<item name="android:windowNoTitle">false</item>
<item name="android:windowBackground">#color/transparent</item>
</style>
<style name="CustomWindowTitleBackground">
<item name="android:background">#drawable/button_black</item>
</style>
put this code in style.xml and just change theme attribute of application tag in AndroidManifest.xml like
<application android:icon="#drawable/ic_launcher"
android:theme="#style/CustomTheme">
.
Thats it.
I am using Android Navigation bar in my project,
I want to change the top color in action bar to something red, How can i do that?
I have something like this,
and i want something like this,
how can i achieve that?
You can define the color of the ActionBar (and other stuff) by creating a custom Style:
Simply edit the res/values/styles.xml file of your Android project.
For example like this:
<resources>
<style name="MyCustomTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBarTheme</item>
</style>
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
Then set "MyCustomTheme" as the Theme of your Activity that contains the ActionBar.
You can also set a color for the ActionBar like this:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED)); // set your desired color
Taken from here: How do I change the background color of the ActionBar of an ActionBarActivity using XML?
In general Android OS leverages a “theme” to allow app developers to globally apply a universal set of UI element styling parameters to Android applications as a whole, or, alternatively, to a single Activity subclass.
So there are three mainstream Android OS “system themes,” which you can specify in your Android Manifest XML file when you are developing apps for Version 3.0 and later versions
I am referring the (APPCOMPAT)support library here:--
So the three themes are
1. AppCompat Light Theme (Theme.AppCompat.Light)
AppCompat Dark Theme(Theme.AppCompat),
And a hybrid between these two ,AppCompat Light Theme with the Darker ActionBar.( Theme.AppCompat.Light.DarkActionBar)
AndroidManifest.xml and see the tag, the android theme is mentioned as:-- android:theme="#style/AppTheme"
Open the Styles.xml and we have base application theme declared there:--
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
We need to override these parent theme elements to style the action bar.
ActionBar with different color background:--
To do this we need to create a new style MyActionBar(you can give any name) with a parent reference to #style/Widget.AppCompat.Light.ActionBar.Solid.Inverse that holds the style characteristics for the Android ActionBar UI element. So definition would be
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/red</item>
</style>
And this definition we need to reference in our AppTheme, pointing to overridden ActionBar styling as--
#style/MyActionBar
Change the title bar text color (e.g black to white):--
Now to change the title text color, we need to override the parent reference parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
So the style definition would be
<style name="MyActionBarTitle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
We’ll reference this style definition inside the MyActionBar style definition, since the TitleTextStyle modification is a child element of an ActionBar parent OS UI element. So the final definition of MyActionBar style element will be
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/red</item>
<item name="titleTextStyle">#style/MyActionBarTitle</item>
</style>
SO this is the final Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- This is the styling for action bar -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/red</item>
<item name="titleTextStyle">#style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
</resources>
For further ActionBar options Menu styling, refer this link
For Android 3.0 and higher only
When supporting Android 3.0 and higher only, you can define the action bar's background like this:
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#ff0000</item>
</style>
</resources>
For Android 2.1 and higher
When using the Support Library, your style XML file might look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
</resources>
Then apply your theme to your entire app or individual activities:
for more details Documentaion
You can change action bar color on this way:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/green_action_bar</item>
</style>
Thats all you need for changing action bar color.
Plus if you want to change the status bar color just add the line:
<item name="android:colorPrimaryDark">#color/green_dark_action_bar</item>
Here is a screenshot taken from developer android site to make it more clear, and here is a link to read more about customizing the color palete
Another possibility of making.
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
I can change ActionBar text color by using titleTextColor
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="titleTextColor">#333333</item>
</style>
Custom Color:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
Custom Style:
<style name="Theme.AndroidDevelopers" parent="android:style/Theme.Holo.Light">
<item name="android:selectableItemBackground">#drawable/ad_selectable_background</item>
<item name="android:popupMenuStyle">#style/MyPopupMenu</item>
<item name="android:dropDownListViewStyle">#style/MyDropDownListView</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabStyle</item>
<item name="android:actionDropDownStyle">#style/MyDropDownNav</item>
<item name="android:listChoiceIndicatorMultiple">#drawable/ad_btn_check_holo_light</item>
<item name="android:listChoiceIndicatorSingle">#drawable/ad_btn_radio_holo_light</item>
</style>
For More: Android ActionBar
As I was using AppCompatActivity above answers didn't worked for me. But the below solution worked:
In res/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
</style>
PS: I've used colorPrimary instead of android:colorPrimary
in the style.xml add this code
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyAction</item>
</style>
<style name="MyActionBarTheme" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#333333</item>
</style>
</resources>
Use this - http://jgilfelt.github.io/android-actionbarstylegenerator/
Good tool to customize your actionbar with a live preview in couple of minutes.