How to change the background color of a TextView inside an ActionMode

I have TextViewinside my action bar, for example:

non-action mode image

I have a logic that is configured so that it is imposed Toolbaron ActionModethat a long time I click on an element in a linked ListView. It looks like this:

action mode image

It is basically right, except for the annoying background around the header TextViewin ActionMode.

I tried several things, from changing the style Widget.ActionModein my styles.xmlfile, to actually setting actionModeStylea specific style. However, nothing affects this TextView.

I read How to change the background color of an ActionMode in Android , but it doesn’t look like everything I do affects this background.

. , - Android, . 1/3 , . - , ?

styles.xml( ):

<style name="Material" parent="Theme.AppCompat.Light">
    <item name="textHeaderMaxLines">@integer/text_header_max_lines</item>
    <item name="trackAbstractMaxLines">@integer/track_abstract_max_lines</item>
    <item name="activatableItemBackground">@drawable/activatable_item_background</item>

    <!-- ActionMode Styles -->
    <item name="android:windowActionModeOverlay">true</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeBackground">@color/app_green_dark</item>
</style>
<style name="Material.AppBar" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColorPrimaryInverse">@android:color/white</item>
    <item name="android:actionMenuTextColor">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:background">@color/app_green</item>
    <item name="android:color">@android:color/white</item>
    <item name="android:textColor">@android:color/white</item>
</style>
<style name="Material.AppBar.ActionMode" parent="Widget.AppCompat.Light.ActionMode.Inverse">
    <item name="android:background">@color/app_green_dark</item>
</style>
+4
1

, backgroud:

Java:

  • hierarchyviewer.bat , ActionMode, : ( : action_bar_title, : action_bar_subtitle)

enter image description here

2. TextView , , , !

/**
 * To hack the title and subTitle in ActionMode
 */
private void hack() {
    int titleID = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    View titleView = getWindow().getDecorView().findViewById(titleID);
    if ((titleView != null) && (titleView instanceof TextView)) {
        ((TextView) titleView).setBackgroundColor(Color.RED);
    }
    int subTitleID = Resources.getSystem().getIdentifier("action_bar_subtitle", "id", "android");
    View subTitleView = getWindow().getDecorView().findViewById(subTitleID);
    if ((subTitleView != null) && (subTitleView instanceof TextView)) {
        ((TextView) subTitleView).setBackgroundColor(Color.GREEN);
    }
}

3. , ActionMode create, : in onCreateActionMode(). :

enter image description here

:

android:actionModeStyle , android:background, , :

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionModeBackground">@android:color/white</item>
    <item name="android:actionModeStyle">@style/TestActionModeStyle</item>
</style>

<style name="TestActionModeStyle">
    <item name="android:titleTextStyle">@style/TestTitleStyle</item>
    <item name="android:subtitleTextStyle">@style/TestSubTitleStyle</item>
</style>

<style name="TestTitleStyle">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:background">@android:color/holo_red_dark</item>
</style>

<style name="TestSubTitleStyle">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:background">@android:color/holo_green_dark</item>
</style>

Edit:

:

, ActionMode, , :

@Override
public boolean onCreateActionMode(ActionMode pMode, Menu pMenu) {
    RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_layout, null);
    findView(...);
    initView(...);
    pMode.setCustomView(layout);
    return true;
}
+2

All Articles