Changing Android action bar background colour - android

For some reason this setup just gives me the default action bar background colour.
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#d73830</item>
</style>
</resources>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
...
</application>
In activity_main.xml my theme is set to 'AppTheme'.

You have wrong parent of MyActionBar.
It must be from AppCompat, like next:
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">#color/red</item>
<item name="background">#color/red</item>
</style>

What you can do is to use hash code of colour.Here is how.Go to this web page
http://www.color-hex.com
Choose a colour you wish and after copy the code of that colour and later paste into your background section.In general lets say you want your background colour of your screen to be red so you must declare it in xml layout file like this
android:background = "#FF0508"/>
You can declare this code almost everywhere where you want to change exact things colour and for my opinion its the easiest way.Why not?you got a webpage where you can get colour code and you can use it with a single line everywhere.Delicios isn't it?

Related

Action Bar android change height [duplicate]

I would like to change the action bar size. I have tried the following coding.
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/CustomActionBar</item>
</style>
<style name="CustomActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<!-- <item name="android:background">#drawable/action_bar_style</item> -->
<item name="android:actionBarSize">15dp</item>
<item name="android:background">#drawable/header_bar</item>
</style>
But the action bar size didn't change. Is there another way? I am using api level 11.
Thanks.
Use height attribute, actionBarSize if for something else.
<item name="android:height">#dimen/bar_height</item>
Explanantion:
From source code of ActionBar:
mContentHeight = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
We can see that R.styleable.ActionBar_height is being used for height. Stylable property names are generated as component_attribute (If you have ever used a custom stylable view, you'd have notice this). Hence, Actionbar is the name of component and height is the name of the attribute to use. Since this is a system attribute, hence defined under android namespace.
Update Dec-2014:
AppCompat library is now provided to extend support for latest ActionBar (or Toolbar) and theme support to old android versions. Below is an example of such an application theme /res/values/styles.xml:
<resources>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<!-- native widgets will now be "tinted" with accent color -->
<item name="colorAccent">#color/accent</item>
<!--Action bar style-->
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:height">#dimen/bar_height</item>
<item name="height">#dimen/bar_height</item>
</style>
<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textSize">#dimen/bar_text_size</item>
<item name="android:textColor">#color/bar_text_color</item>
</style>
</resources>
This style can now be set as app theme by using android:theme="#style/AppTheme" in <application> tag of the AndroidManifest.xml.
Note the use of duplicate entries
<item name="android:actionBarStyle">
<item name="actionBarStyle">
The ones without android namespace are there for supporting both compatibility library and native attributes.Some of these attributes didn't exist under android namespace on older versions and belong to support library.
In some other places, you'll need to use app namespace (xmlns:app="http://schemas.android.com/apk/res-auto"), for example app:showAsAction="always" in menu xml files.
Update Apr 2015
AppCompat Library v22 is also available. Read through the article to know what's new.
Simply put actionBarSize item under MyTheme style, like this:
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarSize">15dp</item>
<item name="actionBarSize">15dp</item>
...
</style>
Explanation:
In R.styleable we see that R.styleable.Theme_actionBarSize is a styleable attribute defined at Theme level.
Also, from source code res/values/styles.xml we see how actionBarSize is used to set height:
<style name="Widget.ActionBar">
...
<item name="android:height">?android:attr/actionBarSize</item>
Add this to the custom theme style XML that you are referencing from your manifest file:
<item name="android:windowTitleSize">58dip</item>
For instance if your manifest file looks something like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
**android:theme="#style/AppTheme"** >
Your theme style should be -at least- like this:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTitleSize">54dip</item>
</style>
S.D.'s solution does not work for me.
I used AppCompat v-21 library in my case.
And I just add
android:layout_height="#dimen/actionBarSize"
android:minHeight="#dimen/actionBarSize"
to my layout file and it works.
Make sure to add S.D.'s code to styles.xml and then add
android:theme="#style/thin_ab"
to the <application> element in the main manifest. (I'm a noob and it took me about 2 hours to find that out.)

Change button background color in entire project in Android

I'm making an Android app, but I cannot change the default background color of all buttons in the project.
The only way I can do it is by changing them one by one.
Is there any other way?
Set buttonStyle in your Theme.
like following:
(I assume that you use AppCompat)
In your style.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
...
<item name="buttonStyle">#style/YourButtonStyle</item>
<!-- For preview -->
<item name="android:buttonStyle">#style/YourButtonStyle</item>
</style>
<style name="YourButtonStyle" parent="#style/Widget.AppCompat.Button">
<!-- Set background -->
<item name="android:background">#drawable/your_button_drawable</item>
</style>
</resources>
In your AndroidManifest.xml:
<application
...
android:theme="#style/AppTheme" >

Android appcompat theme always shows black color actionbar

I generated actionbar style using this web based tool which is very common
http://jgilfelt.github.io/android-actionbarstylegenerator/
I've 'android-support-v7-appcompat' as an individual project in my workspace. I dumped all the contents of the res folder generated by downloading the zip file from this tool into this project.
I used a dark blue kinda color for the action bar and context menu (pop up).
However, I'm not able to achieve the custom look from this theme. Most of the elements / components show a default look.
The actionbar is black (instead of blue) and the pop menu is grey (instead of light blue)
My style.xml from the value folder
<style name="Theme.dinemobileandro" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">#drawable/selectable_background_dinemobileandro</item>
<item name="popupMenuStyle">#style/PopupMenu.dinemobileandro</item>
<item name="dropDownListViewStyle">#style/DropDownListView.dinemobileandro</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.dinemobileandro</item>
<item name="actionDropDownStyle">#style/DropDownNav.dinemobileandro</item>
<item name="actionBarStyle">#style/ActionBar.Solid.dinemobileandro</item>
<item name="actionModeBackground">#drawable/cab_background_top_dinemobileandro</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_dinemobileandro</item>
<item name="actionModeCloseButtonStyle">#style/ActionButton.CloseMode.dinemobileandro</item>
<!-- Light.DarkActionBar specific -->
<item name="actionBarWidgetTheme">#style/Theme.dinemobileandro.Widget</item>
</style>
I've made the necessary changes in my manifest file as well
<application
android:name="com.railyatri.in.mobile.MainApplication"
android:allowBackup="true"
android:allowClearUserData="true"
android:enabled="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.dinemobileandro" >
I've followed the steps mentioned in this blog closely and it worked for me previously for a sherlock theme.
http://java.dzone.com/articles/creating-custom-android-styles
However, this time, I migrated to appcompat and it stopped working. I've searched through SO and Google a lot and tried a couple of things suggested but all in vain.
Please help. Thanks in advance.
With AppCompat v21 the actionbarstylegenerator is deprecated.
You can use something like this to customize the ActionBar (or better the Toolbar).
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat's color theming attrs -->
<item name="colorPrimary">#color/my_color</item>
<item name="colorPrimaryDark">#color/my_darkercolor</item>
</style>
You can find more info in the official blog:
http://android-developers.blogspot.it/2014/10/appcompat-v21-material-design-for-pre.html
Have your already tried to change on your Java file?
Something like this:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setTitle(Html.fromHtml("<font color=\"grey\">" + getString(R.string.app_name) + "</font>"));
getSupportActionBar().setBackgroundDrawable(..);
On Android manifest:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.dinemobileandro" >
On styles xml:
<style name="Theme.dinemobileandro" parent="AppTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/blue</item>
<item name="background">#drawable/blue</item>
On drawables xml:
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<drawable name="blue">#6189C5</drawable>
</resources>
The toolbar on activity is blue (#6189C5) compiling with API 21
Completed with the info in this answer (like Apurva appointed)
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/my_actionbar_color</item>
<item name="colorPrimaryDark">#color/my_actionbar_dark_color</item>
<!-- if you need customization -->
<item name="actionBarStyle">#style/MyActionBarStyle</item>
</style>
Remember to use
parent="Theme.AppCompat.Light"
not
parent="#style/Theme.AppCompat.Light"

Difference between style.xml and theme.xml android studio?

Hey I'm trying to change the background of my action bar but I'm not quiet sure I understand the structure of the style files. So basically I have style.xml which is to pass on some elements a desired style. Then I've Theme.xml which is to have a group of styles in it, correct ? How do you link the two in other words how do I tell the theme I want the specified style in it ? I can't manage to change the background of the action bar so here is my code:
style.xml:
<!-- Base application theme. -->
<style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorBackground">#color/color_lightBlue</item>
</style>
</resources>
theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomTheme" parent="Theme.AppCompat.Light">
</style>
</resources>
and wtf is v11/style ?
Whether you put the attributes in styles.xml or theme.xml, they have the same effect finally if you apply the theme to your app or activity. In your AndroidManifest.xml, in order to let the theme have the effect you want, you should apply your customized theme like following to your app.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >

How to change action bar size

I would like to change the action bar size. I have tried the following coding.
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/CustomActionBar</item>
</style>
<style name="CustomActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<!-- <item name="android:background">#drawable/action_bar_style</item> -->
<item name="android:actionBarSize">15dp</item>
<item name="android:background">#drawable/header_bar</item>
</style>
But the action bar size didn't change. Is there another way? I am using api level 11.
Thanks.
Use height attribute, actionBarSize if for something else.
<item name="android:height">#dimen/bar_height</item>
Explanantion:
From source code of ActionBar:
mContentHeight = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
We can see that R.styleable.ActionBar_height is being used for height. Stylable property names are generated as component_attribute (If you have ever used a custom stylable view, you'd have notice this). Hence, Actionbar is the name of component and height is the name of the attribute to use. Since this is a system attribute, hence defined under android namespace.
Update Dec-2014:
AppCompat library is now provided to extend support for latest ActionBar (or Toolbar) and theme support to old android versions. Below is an example of such an application theme /res/values/styles.xml:
<resources>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<!-- native widgets will now be "tinted" with accent color -->
<item name="colorAccent">#color/accent</item>
<!--Action bar style-->
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:height">#dimen/bar_height</item>
<item name="height">#dimen/bar_height</item>
</style>
<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textSize">#dimen/bar_text_size</item>
<item name="android:textColor">#color/bar_text_color</item>
</style>
</resources>
This style can now be set as app theme by using android:theme="#style/AppTheme" in <application> tag of the AndroidManifest.xml.
Note the use of duplicate entries
<item name="android:actionBarStyle">
<item name="actionBarStyle">
The ones without android namespace are there for supporting both compatibility library and native attributes.Some of these attributes didn't exist under android namespace on older versions and belong to support library.
In some other places, you'll need to use app namespace (xmlns:app="http://schemas.android.com/apk/res-auto"), for example app:showAsAction="always" in menu xml files.
Update Apr 2015
AppCompat Library v22 is also available. Read through the article to know what's new.
Simply put actionBarSize item under MyTheme style, like this:
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarSize">15dp</item>
<item name="actionBarSize">15dp</item>
...
</style>
Explanation:
In R.styleable we see that R.styleable.Theme_actionBarSize is a styleable attribute defined at Theme level.
Also, from source code res/values/styles.xml we see how actionBarSize is used to set height:
<style name="Widget.ActionBar">
...
<item name="android:height">?android:attr/actionBarSize</item>
Add this to the custom theme style XML that you are referencing from your manifest file:
<item name="android:windowTitleSize">58dip</item>
For instance if your manifest file looks something like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
**android:theme="#style/AppTheme"** >
Your theme style should be -at least- like this:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTitleSize">54dip</item>
</style>
S.D.'s solution does not work for me.
I used AppCompat v-21 library in my case.
And I just add
android:layout_height="#dimen/actionBarSize"
android:minHeight="#dimen/actionBarSize"
to my layout file and it works.
Make sure to add S.D.'s code to styles.xml and then add
android:theme="#style/thin_ab"
to the <application> element in the main manifest. (I'm a noob and it took me about 2 hours to find that out.)

Categories

Resources