19

I am aware of android:actionModeBackground that can be used in XML themes.

Is there a way to set this background in code?

Basically I need the ActionMode equavalent of

getActionBar().setBackgroundDrawable(drawable);
Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • Using reflection would probably work, but it means getting your hands dirty with stuff you're not really supposed to touch. Also, there are no guarantees about forward and/or backward compatibility for such a solution. Anyways, just a thought. – MH. Jun 28 '13 at 07:37
  • You can find the answer in this question: http://stackoverflow.com/questions/6556116/how-can-i-customize-the-action-modes-color-and-text – Bart Burg Jun 28 '13 at 13:51
  • You'll have to use reflection. [How to animate background of ActionMode of the ActionBar?](http://stackoverflow.com/questions/23457709/how-to-animate-background-of-actionmode-of-the-actionbar) – adneal May 10 '14 at 13:20
  • Strange how my 11 month old question can be a "asked before and already has an answer" yet the question I am supposedly duplicating is only days old. – Kuffs May 11 '14 at 08:33
  • check this [SO](http://stackoverflow.com/questions/23457709/how-to-animate-background-of-actionmode-of-the-actionbar) answer – silwar Jun 10 '15 at 09:51
  • Then how it is implemented in Gmail app? Default - red, selection mode - gray, search mode - white. I don't believe Google used reflection for this in their app. – surlac Apr 20 '16 at 20:46
  • Since asking the question, I solved my issue by making my own actionmode with a toolbar. – Kuffs Apr 20 '16 at 20:50

3 Answers3

4

In Kotlin using Android Studio 3.4.2:

(actionMode as? StandaloneActionMode).let {
    val contextView = it?.javaClass?.getDeclaredField("mContextView")
    contextView?.isAccessible = true

    val standActionMode = contextView?.get(it)
    val color = ContextCompat.getColor(context, R.color.colorResId)
    (standActionMode as? View)?.setBackgroundColor(color)
}

To cast actionMode to StandaloneActionMode, don't forget to import ActionMode from androidx.appcompat.view.ActionMode and not from android.view.ActionMode.

pableiros
  • 14,932
  • 12
  • 99
  • 105
2

I figured out with reflection help. Because I dont have an actionbar

public static void setActionModeBackgroundColor(ActionMode actionMode, int color) {
        try {
            StandaloneActionMode standaloneActionMode = (StandaloneActionMode) actionMode;
            Field mContextView = StandaloneActionMode.class.getDeclaredField("mContextView");
            mContextView.setAccessible(true);
            Object value = mContextView.get(standaloneActionMode);
            ((View) value).setBackground(new ColorDrawable(color));
        } catch (Throwable ignore) {
        }
    }

Also there are 2 implementations of ActionMode : StandaloneActionMode and ActionModeImpl. this example only for First one. For second one it will be same

0

You can get the ActionMode id by using this action_context_bar

   int amId = getResources().getIdentifier("action_context_bar", "id", "android");
   View view= findViewById(amId);
   view.setBackground(actionModeBackground);
mFarouk
  • 11
  • 4