How can I customize the Action Mode's color and text?
Asked Answered
P

8

24

The default action mode (3.0 and up) comes with a green theme and a 'Done' button on the left side. How can I customize these?

Thanks

Publicist answered 2/7, 2011 at 9:16 Comment(0)
G
35

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>
Gluey answered 27/12, 2011 at 0:34 Comment(4)
Do you know the name of the attribute or style which sets the small background around the title and subtitle?Waylan
I am using a custom theme in my application, and as an item I added the following: <item name="android:actionModeSplitBackground">@drawable/cab</item>, which is the background that I define for the CAB when in split mode, but the bottom part still remains with the default look. Do you know what might be the problem or am I doing something wrong?Miserable
Can you please tell me how to use this in app theme? My style.xml: pastie.org/9261136Ephemeral
What's the "backgroundSplit" for?Horsefly
B
26

Solution for my application

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">@color/bg_action_bar</item>
</style>
Brisesoleil answered 18/2, 2014 at 11:58 Comment(2)
Can you tell me what to do for changing the tick image and the seperator?Ephemeral
For supporting pre and post-lollipop devices, you need to add also: <item name="actionModeBackground">@color/bg_action_bar</item>Correia
F
15

Worked on my project

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="actionModeStyle">@style/CustomActionModeStyle</item>
 </style>

Custom ActionMode style

<style name="CustomActionModeStyle" parent="Base.Widget.AppCompat.ActionMode">
        <item name="background">@color/color_primary</item>
        <item name="titleTextStyle">@style/CustomeActionModeTextStyle</item>
</style>

Custom Title ActionMode

<style name="CustomeActionModeTextStyle" parent="TextAppearance.AppCompat.Widget.ActionMode.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/color_primaryText</item>
</style>
Forb answered 27/2, 2017 at 9:40 Comment(0)
R
11

with this code you can change the Background color of Action mode and also change DONE image. Note: you can add your text in your image too! in res/styles.xml:

<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:actionModeBackground">@android:color/white</item>
<item name="android:actionModeCloseDrawable">@drawable/plus</item>

Raynor answered 23/4, 2015 at 15:9 Comment(0)
A
2

You can't really customize it this way because the attribute actionModeStyle is introduced at API level 14. For API levels 11 to 13 you are out of luck.

For API level 14, you can change the style by setting the android:actionModeStyle in your theme.

Amends answered 30/12, 2011 at 6:52 Comment(0)
T
2

Here is my approach with Java code:

private void customizeActionModeCloseButton(String title, int iconID) {
          int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");    
          View v = findViewById(buttonId);
          if (v == null) {
             buttonId = R.id.abs__action_mode_close_button;
             v = findViewById(buttonId);
          }
          if (v == null)
             return;
          LinearLayout ll = (LinearLayout) v;
          if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
             //custom icon
             ImageView img = (ImageView) ll.getChildAt(0);
             img.setImageResource(iconID);
             //custom text
             TextView tv = (TextView) ll.getChildAt(1);
             tv.setText(title);
             tv.setTextColor(Color.WHITE);
          }
       }
Talamantes answered 23/7, 2013 at 8:43 Comment(0)
H
1

Updated answer for both pre- and post-Lollipop devices. You must remove the android: prefix to get it to work on Lollipop+ devices, like so:

styles.xml:

<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>

v21/styles.xml:

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

I'd also recommend having your style with parent="@style/Widget.AppCompat.ActionMode" set, so you inherit the attributes you don't care about overriding.

Herstein answered 10/10, 2016 at 22:54 Comment(0)
Q
0

Here's an AppCompat (i.e. using startSupportActionMode) solution for temporarily customizing (customising) the CAB done button's image. Temporarily since it's desirable to change it back to use it's typical image so that when Text Selection kicks in it looks appropriate.

https://gist.github.com/coreform/36ed98f98668f2e90c6a

Quinta answered 8/1, 2015 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.