6

I am setting up the actionModeCloseDrawable icon as below

<item name="actionModeCloseDrawable">@drawable/ic_actionbar_back</item>

But I want to change it pro-grammatically on paticular event.

Is there any way to do so?

I have tried defining two styles and changing it on runtime, but that also didn't work.

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
  • You have to create 2 themes and [switch between themes](https://stackoverflow.com/questions/2482848/how-to-change-current-theme-at-runtime-in-android) upon particular event. – azizbekian Jun 18 '18 at 07:01
  • Theme can be set before `SetContentView()`. I am not switching activity. I want to achieve it in the opened Activity. – Vir Rajpurohit Jun 18 '18 at 07:10
  • `actionModeCloseDrawable` is a theme attribute. As far as I am concerned, there is no way to change the attribute without restarting activity. – azizbekian Jun 18 '18 at 07:11
  • I am using it with `contextual Actionbar` and MULTI_MODE selection of `ListView`. So on some particular event I want to change the drawable of `actionModeCloseDrawable`. Hence restarting Activity doesn't seem to be feasible. – Vir Rajpurohit Jun 18 '18 at 07:15
  • I get your use-case, I'm just telling whether it is achievable or no. – azizbekian Jun 18 '18 at 07:18

1 Answers1

3

Before enter image description here

Code

 new Handler(Looper.myLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            ((AppCompatImageView)getActivity().findViewById(R.id.action_mode_close_button)).setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_person_white_24dp));
        }
    },5000);

After enter image description here

Assumption & State:

  1. You are using support action mode.
  2. In my example, ActionBar is a part of Fragment hence getActivity().findViewById(int resId)
  3. I have used Handler to simulate the delay of an event you are talking about.

Hope this helps.

Rahul
  • 4,699
  • 5
  • 26
  • 38