16

i am creating an android app in which support level api is 7 so i am using sherlock actionbar. I am using action mode in it. Issue is i want to change the background of action mode. So i have tried

    <item name="android:background">@color/something</item>
    <item name="android:backgroundStacked">@color/something</item>
    <item name="android:backgroundSplit">@color/something</item>

these style solution's available but none of them works. enter image description here

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Love Garg
  • 406
  • 1
  • 5
  • 17

9 Answers9

35

In my case i resolved with this. First i created a style to define the actionModeBackground:

<style name="MyTheme.ActionMode" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">#FFFFFF</item>
</style>

Then i add the style into my base application theme:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionModeStyle">@style/MyTheme.ActionMode</item>
</style>

Hope it helps!!!

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
Kokusho
  • 1,113
  • 1
  • 9
  • 14
  • 13
    For supporting pre and post-lollipop devices, you need to add also: @color/bg_action_bar – Eduard Apr 29 '15 at 20:56
  • 4
    You can also just set actionModeBackground directly on the Theme as well. Need both the prefixed and unprefixed versions to support 4.x and 5.x respectively. – cliff.meyers Nov 03 '15 at 19:18
  • 1
    this #FFFFFF doesn't work for me. this @color/bg_action_bar does! Thanks Kokusho – driftwood Nov 05 '18 at 02:22
  • Pre-lollipop: also add `actionModeStyle` (without `android:` prefix). If using appcompat Toolbar, you need to style `background` directly; see: https://stackoverflow.com/q/28374774 – geekley May 03 '20 at 10:44
18

if you want change the color of ActionBar just do this:

 ActionBar bar = getActionBar();
 bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

see the following link for more info

and if you using ActionMode This is the style used for any ActionMode. You'll need to create your own style to customize it

<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>

more info in this site

see this too

Edit

for pre-Honeycomb see this please

maybe this or this helped you

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
16

If you are using simple Action Bar then try this,

ActionBar abar = getActionBar();
abar.setBackgroundDrawable(new ColorDrawable(0xff123456));

But if you are using Actionbar share lock Lib then try this

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff123456));

ColorDrawable(0xff123456) - should be your color code.

Drawable/styles.xml

 <item name="android:actionModeBackground">@drawable/actionbar_background</item>
 <item name="actionModeBackground">@drawable/actionbar_background</item>

Change Theme as per API level

If you want to change as per the API level then you should go for this

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • try [this](http://stackoverflow.com/questions/6556116/how-can-i-customize-the-action-modes-color-and-text)& [this](http://stackoverflow.com/questions/12711612/styling-sherlock-actionmode-background) – Chintan Khetiya Dec 25 '13 at 07:09
  • 1
    hi @chntan i tried this but all of them working for api above 11 nothing for api 7 please help me if you have something for api 7 – Love Garg Dec 25 '13 at 08:38
  • As i have mention you in answer that , you should use [Actionbarshare lock Lib](http://actionbarsherlock.com/) for below 11 API. because ActionBar is not for 11 and higher. – Chintan Khetiya Dec 25 '13 at 08:59
  • yes i am using actionbarsherlock but i have to change background of action mode in api 7. Now its changing in api 11 and above – Love Garg Dec 25 '13 at 09:03
  • Okay, got it, mean you want to change in only 7, for 11 it should be different ? is it like this ? – Chintan Khetiya Dec 25 '13 at 09:04
4

If you want to do it programmatically (supporting AppCompat) you need to look for icon parent view. This code is tested from 2.3.7 to 6.0.1. You need to check on lower API... .

first get your activity decor view

final ViewGroup  decorView = (ViewGroup) getActivity().getWindow().getDecorView();

second set the color in onPrepareActionMode. You can also set the back icon or other elements here

@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) 
{

     decorView.postDelayed(new Runnable() {

     @Override
     public void run() 
     {
        int buttonId = getResources().getIdentifier("action_mode_close_button", "id", "android");

        View v = decorView.findViewById(buttonId);
        if (v == null)
        {
           buttonId = R.id.action_mode_close_button;
            v = decorView.findViewById(buttonId);   
        }

        if (v != null)
        {                         
           ((View)v.getParent()).setBackgroundColor(Color.red /*your color here*/);
        }               
     }   
     }, 500);}
Simas
  • 43,548
  • 10
  • 88
  • 116
Colateral
  • 1,736
  • 2
  • 18
  • 22
2

Override this style in your custom theme:

<style name="Base.Widget.AppCompat.ActionMode" parent="">
    <item name="background">?attr/actionModeBackground</item>
    <item name="backgroundSplit">?attr/actionModeSplitBackground</item>
    <item name="height">?attr/actionBarSize</item>
    <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
    <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle</item>
    <item name="closeItemLayout">@layout/abc_action_mode_close_item_material</item>
</style>

For example:

<style name="LywActionMode" parent="Base.Widget.AppCompat.ActionMode">
    <item name="background">@color/lyw_primary_color</item>
    <item name="backgroundSplit">@color/lyw_primary_color</item>
</style>

<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light">
    ...
    <item name="actionModeStyle">@style/LywActionMode</item>
</style>
Arkadiusz Cieśliński
  • 5,307
  • 3
  • 23
  • 19
1

This is the style used for any ActionMode, I pulled it from the SDK. You'll need to create your own style to customize it. It's really easy to do. If you've never done anything like this before, you should read through this post on customizing the ActionBar. It explains everything you'll need to know.

<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>
M D
  • 47,665
  • 9
  • 93
  • 114
1

For those who still struggle, and none of the existing solutions helps, please make sure that in your layout with toolbar you have this:

app:popupTheme="@style/Theme.ToolBar"
android:popupTheme="@style/Theme.ToolBar"

and you don't have

app:theme="@style/Theme.ToolBar"

Since they will rise a conflict and your customization will not work.

Roger Alien
  • 3,040
  • 1
  • 36
  • 46
1

The simplest way to do it

ActionBarContextView actionBar = getWindow().getDecorView().findViewById(R.id.action_mode_bar);
    actionBar.setBackgroundColor(WhateverColorYouWant);
Eugene Sianko
  • 79
  • 1
  • 9
-1

I wanna make more clearer this answer. First create custom background "storage_list_header_shape.xml" in your drawable folder.

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle" >

   <gradient
      android:angle="270"
      android:startColor="@android:color/holo_orange_light"
      android:centerColor="@android:color/holo_orange_dark"
      android:endColor="@android:color/holo_orange_light" />

   <size android:height="3dp" />

 </shape>

Second, create custom style in your style.xml

<style name="customActionModeTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">@drawable/storage_list_header_shape</item>
</style>

Third, call the style in your manifest.

<activity
     android:name="com.javacafe.activities.Main"
     android:launchMode="singleTop"
     android:screenOrientation="sensorLandscape"
     android:theme="@style/customActionModeTheme" >
</activity>
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72