Search code examples
androidandroid-toolbarcontextual-action-barandroid-statusbar

How to make sure color change happens simultanously on both status bar and contextual action moded toolbar


Currently, I'm following suggestion from https://stackoverflow.com/a/29748810/72437 , in order to change status bar color, when entering contextual action mode.

However, from the following video, https://www.youtube.com/watch?v=2Ra56_eh7uk , we can observe that color change always happen at status bar, only then color change on toolbar will follow.

Is there any way, to make color change happens simultaneously, on both status bar and toolbar.

@SuppressLint("NewApi")
private class ModeCallback implements ListView.MultiChoiceModeListener {

    public boolean onCreateActionMode(android.view.ActionMode mode,
            android.view.Menu menu) {
        ...

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Activity activity = getActivity();
            if (activity != null) {
                activity.getWindow().setStatusBarColor(actionModeStatusBarColor);
            }
        }

        return true;
    }

    public boolean onPrepareActionMode(android.view.ActionMode mode, android.view.Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {
        switch (item.getItemId()) {
            ....
        }

        return false;
    }

    public void onDestroyActionMode(android.view.ActionMode mode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Activity activity = getActivity();
            if (activity != null) {
                activity.getWindow().setStatusBarColor(colorPrimaryDark);
            }
        }
    }
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        this.getListView().setMultiChoiceModeListener(new ModeCallback());
        ...

Solution

  • This is caused because there is animation for switching views on the Toolbar for the normal display and contextual display. You see it because there is no animation applied when switching status bar colors.

    I dig around in the android source ActionBar implementation uses 500 seconds to switch between contextual and normal view. See this for animation duration in android source.

    Probably you need to perform ValueAnimator with ArgbEvaluatior that will animate color changes to the status bar. Run this animation when you destroy the ActionMode. Duration should be 500 seconds or config_longAnimTime

    I hope this helps. :)