Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting ActionMode Background programmatically

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);
like image 213
Kuffs Avatar asked Jun 13 '13 14:06

Kuffs


1 Answers

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

like image 104
Volodymyr Machekhin Avatar answered Sep 18 '22 22:09

Volodymyr Machekhin