Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we show a back button instead of done(checkmark) button in the contextual action bar on Android

I am getting this by default enter image description here

I want this

enter image description here

This should be trivial enough but I can't find anything related on Android docs.

private void setupContextualBar()
    {
        mActionModeCallback = new ActionMode.Callback()
        {
            // Called when the action mode is created; startActionMode() was called
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) 
            {
                MenuInflater inflater = getActivity().getMenuInflater();
                inflater.inflate(R.menu.my_menu , menu);
                mCABMenu = menu;
                return true;
            }

            // Called each time the action mode is shown. Always called after onCreateActionMode, but
            // may be called multiple times if the mode is invalidated.
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) 
            {
                updateContextualBar();
                return true;
            }

            // Called when the user selects a contextual menu item
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) 
            {
                int menuItemId = item.getItemId();
                boolean eventConsumed = false;
                switch (menuItemId)
                {
                     //handle cases here
                }
                if (eventConsumed)
                {
                     updateContextualBar();
                }
                return eventConsumed;
            }

            // Called when the user exits the action mode
            @Override
            public void onDestroyActionMode(ActionMode mode) 
            {
                mActionMode = null;
            }
        };
like image 929
Varun Bhatia Avatar asked Dec 12 '14 07:12

Varun Bhatia


2 Answers

You can change this button image by using a custom theme that is associated with your activity, as follows:

<style name="MyCustomTheme" parent="MyUsualTheme">
    <item name="android:actionModeCloseDrawable">@drawable/myBackDrawable</item>
</style>

AndroidManifest.xml:

 <activity
        android:name="MyActivity"
        android:theme="@style/MyCustomTheme"
  ...

I've researched but found no way to change action_mode_close_button programmatically (there are hacks that try to do this but they have serious potential side effects). It appears that the only reliable/safe way to change this image is via a theme change.

like image 170
Steve B Avatar answered Oct 01 '22 22:10

Steve B


If you are using default Action Mode: startActionMode(...)

You can use:

<style name="MyCustomTheme" parent="MyUsualTheme">
    <item name="android:actionModeCloseDrawable">@drawable/myBackDrawable</item>
</style>

But if you are using Android Support V7 Action Mode: startSupportActionMode(...)

You should use:

<style name="MyCustomTheme" parent="MyUsualTheme">
    <item name="actionModeCloseDrawable">@drawable/myBackDrawable</item>
</style>
like image 37
Frank Nguyen Avatar answered Oct 01 '22 22:10

Frank Nguyen