Jeffrey Blattman
Jeffrey Blattman

Reputation: 22637

Action mode close button text color

I see to set the text color of the action mode "done" / "close" button. This is what I've tried:

<item name="android:actionModeCloseButtonStyle">@style/ActionModeCloseButton</item>
....
<style name="ActionModeCloseButton" parent="android:style/Widget.Holo.ActionButton.CloseMode">
    <item name="android:textColor">@android:color/white</item>
</style>

but is has no effect.

enter image description here

Note that on JB, it's enough that I make the ActionModeCloseButton style's parent the regular holo theme. It works fine there (without the textColor setting even).

Any ideas?

Upvotes: 12

Views: 7442

Answers (2)

Trinimon
Trinimon

Reputation: 13967

Since the layout for the action mode close button doesn't provide a color attribute for the text view, there is no way to set this color in a custom theme. Instead the only ay I found was to overwrite the text color in the onPrepareActionMode() method of my derived ActionMode class:

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... none) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void none) {
            if (activity != null) {
                LinearLayout layout = (LinearLayout) activity
                            .findViewById(R.id.abs__action_mode_close_button);

                if (layout == null) {
                    int id = Resources.getSystem().getIdentifier(
                                      "action_mode_close_button", "id", "android");
                    layout = (LinearLayout) activity.findViewById(id);
                }

                if (layout != null && layout.getChildCount() > 1) {
                    TextView label = (TextView) layout.getChildAt(1);
                    if (label != null) label.setTextColor(Color.RED);
                }
            }
        }
    }.execute();

    return false;
}

Worked with two devices pre and post Android 4.0.

Upvotes: 0

Jelle
Jelle

Reputation: 919

First of all, the textview "Done" is only visible on large devices. Checkout action_mode_close_item.xml in the Android source. So the android:actionModeCloseButtonStyle only applies to the containing view and not the imageview and the textview.

Luckily, the android engineers used publicly accessible attributes to styles the childviews.

  • Use android:actionMenuTextColor to change to textColor of the TextView.
  • Use android:actionModeCloseDrawable to change the drawable of the ImageView

Example:

<style name="MyTheme">
    <item name="android:actionMenuTextColor">#ff000000</item>
    <item name="android:actionModeCloseDrawable">@drawable/my_close_drawable</item>
</style>

Below is a copy of the action_mode_close_item.xml in the layout-large-folder where you can see how the layout is build.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/action_mode_close_button"
        android:focusable="true"
        android:clickable="true"
        android:paddingStart="8dip"
        style="?android:attr/actionModeCloseButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="16dip">
    <ImageView android:layout_width="48dip"
               android:layout_height="wrap_content"
               android:layout_gravity="center"
               android:scaleType="center"
               android:src="?android:attr/actionModeCloseDrawable" />
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:layout_marginStart="4dip"
              android:layout_marginEnd="16dip"
              android:textAppearance="?android:attr/textAppearanceSmall"
              android:textColor="?android:attr/actionMenuTextColor"
              android:textSize="12sp"
              android:textAllCaps="true"
              android:text="@string/action_mode_done" />
</LinearLayout>

Upvotes: 7

Related Questions