How to change ActionMode background color in Android
Asked Answered
P

9

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

Podgy answered 25/12, 2013 at 5:22 Comment(2)
that is a contextual action barJacobean
well @Jacobean i am not using CAB i am creating custom action modePodgy
P
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

Printery answered 25/12, 2013 at 5:23 Comment(4)
thanks for your response now its working fine above api 11 but i need to give support for api 7 alsoPodgy
Pre-honeycomb link is 404Keef
@TimCooke I've edit my post with new link, thanks to mention,Printery
titleTextStyle and subtitleTextStyle are not public.Dizon
T
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!!!

Tonl answered 20/5, 2014 at 20:35 Comment(4)
For supporting pre and post-lollipop devices, you need to add also: <item name="actionModeBackground">@color/bg_action_bar</item>Forgather
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.Lolland
this <item name="android:actionModeBackground">#FFFFFF</item> doesn't work for me. this <item name="actionModeBackground">@color/bg_action_bar</item> does! Thanks KokushoForehanded
Pre-lollipop: also add actionModeStyle (without android: prefix). If using appcompat Toolbar, you need to style background directly; see: https://mcmap.net/q/747220/-unable-to-style-action-mode-when-using-toolbarPsychotic
P
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

Printery answered 25/12, 2013 at 5:23 Comment(4)
thanks for your response now its working fine above api 11 but i need to give support for api 7 alsoPodgy
Pre-honeycomb link is 404Keef
@TimCooke I've edit my post with new link, thanks to mention,Printery
titleTextStyle and subtitleTextStyle are not public.Dizon
B
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

Broadnax answered 25/12, 2013 at 5:30 Comment(5)
try this& thisBroadnax
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 7Podgy
As i have mention you in answer that , you should use Actionbarshare lock Lib for below 11 API. because ActionBar is not for 11 and higher.Broadnax
yes i am using actionbarsherlock but i have to change background of action mode in api 7. Now its changing in api 11 and abovePodgy
Okay, got it, mean you want to change in only 7, for 11 it should be different ? is it like this ?Broadnax
F
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);}
Fredi answered 20/10, 2016 at 19:32 Comment(0)
P
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>
Painful answered 18/3, 2015 at 17:56 Comment(0)
T
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>
Toomin answered 25/12, 2013 at 5:31 Comment(1)
"Cannot resolve symbol Widget" lol. And what is min API of this?Submission
T
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.

Tightlipped answered 23/3, 2017 at 19:28 Comment(0)
C
1

The simplest way to do it

ActionBarContextView actionBar = getWindow().getDecorView().findViewById(R.id.action_mode_bar);
    actionBar.setBackgroundColor(WhateverColorYouWant);
Captivity answered 30/8, 2017 at 8:32 Comment(0)
C
-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>
Cormack answered 4/4, 2014 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.