Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change actionModeCloseDrawable icon dynamically?

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.

like image 680
Vir Rajpurohit Avatar asked Jun 13 '18 06:06

Vir Rajpurohit


1 Answers

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.

like image 51
Rahul Sharma Avatar answered Nov 15 '22 10:11

Rahul Sharma