Android Changing Action Bar color - android

I want to change the Action Bar color, but I don't know how to do it. What should I add to change the style?
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>

This is all you need to change the color of the action bar:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#995544</item>
</style>

That code works for me and it also changes text color
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</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="background">#color/colorPrimary</item>
<item name="android:height">150dp</item>
<item name="height">60dp</item>
</style>
<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textSize">15dp</item>
<item name="android:textColor">#color/colorAccent</item>
</style>

simple a single Line code
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.red)));

If your minSdk is 21 or higher, you can add the following:
<item name="colorPrimary">#color/yourColor</item>
If not then make the following changes:
Your activity must extend AppCompatActivity
Change your theme to #style/Theme.AppCompat.Light.NoActionBar
Add this to the top of your activity xml file:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
Put this in your onCreate method after the super.onCreate call:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
To change the color just replace ?attr/colorPrimary with #color/yourColor

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style
Here colorPrimary attribute helps you to change the color of action bar.
For more details see this fig.

Please try the code below:
<style name="AppCompatTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#ff540e9e</item>
<item name="android:textColor">#ffffff</item>
</style>

At your theme.xml or/and theme.xml (dark), change the following android:statusBarColor which change the to status bar background color, toolbarNavigationButtonStyle change the back button color, actionBarStyle change the background and text color and displayOptions to show the text.
<style name="Theme.Example" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Status bar color. -->
<item name="android:statusBarColor">#color/backgroundDark</item>
<!-- action bar color. -->
<item name="toolbarNavigationButtonStyle">#style/Toolbar.Button.Navigation.Tinted</item>
<item name="actionBarStyle">#style/Actionbar.dark</item>
<item name="displayOptions">showHome|useLogo|showTitle</item>
</style>
<style name="Actionbar.dark" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="background">#color/backgroundDark</item>
<item name="titleTextStyle">#style/Actionbar.darktext</item>
</style>
<style name="Actionbar.darktext" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">#color/white</item>
</style>

You can customize the current theme for your app, in the res/values/styles.xml file try:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
// your custom styles added to theme
<item name="android:actionBarStyle">#style/MyActionBarTheme</item>
</style>
//Cutomise your theme by specifying layout,color, font type etc:
<style name="MyActionBarTheme"
parent="#android:style/Widget.AppCompat.Light.DarkActionBar">
// sets the background color of the activity/app:
<item name="android:background">#33FF33</item>
</style>
Then you will need to add your new custom theme name to your apps Manifest file

Related

Android, set Toolbar style in theme.xml file

I am trying to customize the look of Toolbar in my app.
Target
I would like to set the title color and the background color of the Toolbar.
I read about how Theme and Style works, so I was able to try some solutions.
I found out that title color is managed by app:titleTextColor property and the background color from the android:background attribute.
Premise
To know how to act it is necessary to know how the style of the widget that we need to modify is managed.
The base Toolbar style is defined in the Widget.MaterialComponents.Toolbar style as below.
<style name="Widget.MaterialComponents.Toolbar" parent="Widget.AppCompat.Toolbar">
<item name="titleTextAppearance">?attr/textAppearanceHeadline6</item>
<item name="titleTextColor">?android:attr/textColorPrimary</item>
<item name="subtitleTextAppearance">?attr/textAppearanceSubtitle1</item>
<item name="subtitleTextColor">?android:attr/textColorSecondary</item>
<!-- Overrides minimum height in landscape to avoid headline6 and subtitle1 height concerns. -->
<item name="android:minHeight">#dimen/mtrl_toolbar_default_height</item>
<item name="maxButtonHeight">#dimen/mtrl_toolbar_default_height</item>
</style>
Here the value of titleTextColor is set to ?android:attr/textColorPrimary.
So my first attempt was simply to add the ovverride of android:textColorPrimary attribute in my app theme.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:textColorPrimary">#color/white</item>
</style>
Result: This works.
To also set the background I decided to create a custom Toolbar style where to put the background definition, so I changed my theme as below.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<!-- Customize your theme here. -->
<item name="toolbarStyle">#style/MyToolbarStyle</item>
</style>
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="android:textColorPrimary">#color/white</item>
<item name="android:background">?attr/colorPrimary</item>
</style>
Result: This does not works. The android:background is applied but the android:textColorPrimary not.
Setting the value of titleTextColor directly inside MyToolbarStyle works.
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="titleTextColor">?attr/colorOnPrimary</item>
<item name="android:background">?attr/colorPrimary</item>
</style>
I also tried to set the toolbar theme where I override the Android attribute:textColorPrimary, as below.
<style name="Base_ToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="android:background">?attr/colorPrimary</item>
<item name="android:theme">#style/Base_ToolbarTheme</item>
</style>
<style name="Base_ToolbarTheme">
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
</style>
Result: This does not works too.
Now I am a bit confused from the outcome of my tests.
Why is it not possible to override the android:textColorPrimary in the custom style?
To apply a style you can use (and the toolbarStyle attribute applies this style to all the Toolbar) the style attribute:
<androidx.appcompat.widget.Toolbar
style="#style/MyToolbarStyle"
with:
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="titleTextColor">?attr/colorOnPrimary</item>
<item name="android:background">?attr/colorPrimary</item>
</style>
because titleTextColor is a property defined in the Toolbar.
To apply a theme overlay you can use the android:theme attribute:
<androidx.appcompat.widget.Toolbar
android:theme="#style/MyToolbarThemeOverlay"
with
<style name="MyToolbarThemeOverlay">
<item name="android:textColorPrimary">#color/white</item>
</style>
because android:textColorPrimary is an attribute defined at theme level.
If you want to override a theme attribute directly in a style you have to use the materialThemeOverlay attribute:
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="android:background">?attr/colorPrimary</item>
<item name="materialThemeOverlay">#style/MyToolbarThemeOverlay</item>
</style>
With using the AppCompat Material Toolbar androidx.appcompat.widget.Toolbar, I have made the following definitions to customize the Toolbar in a theme
Given this toolbar in a layout file:
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyAppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:popupTheme="#style/MyAppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
Define a toolbar style in themes.xml:
<style name="MyAppTheme.Toolbar" parent="Widget.MaterialComponents.Toolbar">
<!-- Add custom stuff here -->
<item name="android:background" tools:targetApi="l">#color/dark_primary</item>
<item name="background">#color/dark_primary</item>
</style>
And also add the overlay styles in themes.xml:
<style name="MyAppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="MyAppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
And then apply in app theme:
<style name="MyAppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...
<item name="android:toolbarStyle" tools:targetApi="l">#style/MyAppTheme.Toolbar</item>
<item name="toolbarStyle">#style/MyAppTheme.Toolbar</item>
...
</style>

SnackBar using ActionBar Theme?

I having this weird behavior with the styling of ActionBar and Snackbar.
I have below theme for AppTheme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorWhite</item>
<item name="colorPrimaryDark">#color/colorWhiteDark</item>
<item name="colorAccent">#color/colorGreen</item>
</style>
So, I am having white theme. And my SnackBar looks like this
But, whenever I add custom actionBarTheme, like
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorWhite</item>
<item name="colorPrimaryDark">#color/colorWhiteDark</item>
<item name="colorAccent">#color/colorGreen</item>
<item name="actionBarTheme">#style/ActionbarTheme</item>
</style>
<style name="ActionbarTheme">
<item name="android:textColor">#color/colorBlack</item>
<item name="android:background">#color/colorWhite</item>
</style>
My SnackBar looks like this
Why it is causing this behavior and how to fix it?
Since you are applying following style in your parent
<style name="ActionbarTheme">
<item name="android:textColor">#color/colorBlack</item>
<item name="android:background">#color/colorWhite</item>
</style>
Your text color is black and your background is white.
EDIT:
To fix, you need to set textcolor as white and background black:
<style name="ActionbarTheme">
<item name="android:textColor">#color/colorWhite</item>
<item name="android:background">#color/colorBlack</item>
</style>
change item name background to colorPrimary
<style name="ActionbarTheme">
<item name="android:textColor">#color/colorBlack</item>
<item name="colorPrimary">#color/colorWhite</item>
</style>

How to change text color and background color of action bar items in android

I want to change text color and background color of action bar item which is white here. I searched alot but am not able to change this color.
Please check my code which I am using to customise action bar.
final ActionBar actionBar = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor (Hexa color));
actionBar . setBackgroundDrawable (colorDrawable);
In your "res/values/themes.xml" file, do something like this:
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:popupMenuStyle">#style/MyPopupMenu</item>
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
<!-- Popup Menu Background Color styles -->
<style name="MyPopupMenu"
parent="#android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">#color/Your_color_for_background</item>
</style>
<!-- Popup Menu Text Color styles -->
<style name="TextAppearance">
<item name="android:textColor">#color/Your_color_for_text</item>
</style>
And then in your manifest
<application
android:theme="#style/CustomActionBarTheme"
... >
Check this question and some of its answers for more clarification.
You can customize the action bar like below..
<!-- Base application theme. -->
<style name="Theme.AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/Theme.AppTheme.actionBar</item>
<item name="android:actionBarWidgetTheme">#style/Theme.AppTheme.actionBar.widget</item>
<item name="android:textViewStyle">#style/Theme.AppTheme.textView</item>
</style>
<!-- Actionbar things -->
<style name="Theme.AppTheme.actionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
<item name="android:titleTextStyle">#style/Theme.AppTheme.actionBarTitle</item>
<item name="android:subtitleTextStyle">#style/Theme.AppTheme.actionBarSubtitle</item>
<item name="android:background">#android:color/holo_blue_dark</item>
<item name="android:icon">#drawable/ic_launcher</item>
</style>
<style name="Theme.AppTheme.actionBarTitle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="Theme.AppTheme.actionBarSubtitle" parent="android:TextAppearance.Holo.Widget.ActionBar.Subtitle">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="Theme.AppTheme.actionBar.widget" parent="#android:style/Theme.Holo">
<item name="android:popupMenuStyle">#style/Theme.AppTheme.actionBar.PopupMenu</item>
<item name="android:dropDownListViewStyle">#style/Theme.AppTheme.actionBar.DropDownListView</item>
</style>
<style name="Theme.AppTheme.actionBar.PopupMenu" parent="#android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">#android:color/holo_purple</item>
</style>
<style name="Theme.AppTheme.actionBar.DropDownListView" parent="#android:style/Widget.Holo.ListView.DropDown">
<item name="android:listSelector">#android:color/holo_red_light</item>
</style>
<!-- Standard Widgets -->
<style name="Theme.AppTheme.textView" parent="android:Widget.TextView"></style>
Hope this will help you!

Styling the android action bar?

I'm trying to style the action bar inside my app using the code below.
<resources>
<!-- the theme applied to the application or activity -->
<style name="myTheme" parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:textSize">24sp</item>
</style>
simply put the font should be bigger.. but nope.
Any ideas as to why?
Thanks.
Possibly your theme is not applied to activity. Set it in manifest:
<activity
android:name=".YourActivty"
android:theme="#style/myTheme" />
UPDATE 1
You should change action bar text size in such way (changing titleTextStyle):
<style name="Theme.YourTheme" parent="android:style/Theme">
<item name="android:actionBarStyle">#style/Theme.YourTheme.Styled.ActionBar</item>
</style>
<style name="Theme.YourTheme.Styled.ActionBar">
<item name="android:titleTextStyle">#style/Theme.Viadeo.Styled.YourTheme.TitleTextStyle</item>
</style>
<style name="Theme.YourTheme.Styled.ActionBar.TitleTextStyle" parent="#android:style/Widget.TextView">
<item name="android:textSize">24sp</item>
</style>

can we set actionbar height manually in android theme.light

I am using "Theme.Light" theme for my android project and while setting up a navigation drawer how can i set actionbar size(height).
I used the below code and i got an error..
<style name="Theme.Light.ActionBar" parent="#android:style/Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="actionBarSize">36dip</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
can you please reply to my problem as soon as possible and thank you.
Use height attribute,for actionBarHeight
<item name="android:height">#dimen/bar_height</item>
you have to create style like this:
<!-- 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="titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android: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>
you have to try below code :
<resources>
<style name="Theme.FixedSize" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="actionBarSize">48dip</item>
<item name="android:actionBarSize">48dip</item>
</style>
</resources>
If you are using AppCompat, you can set the "actionBarSize" attribute in your theme (which must inherit from Theme.AppCompat.x). You can also use a Toolbar in your layout and set its layout_height to the desired value.

Categories

Resources