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

4

5

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;
            }
        };
Vivie answered 12/12, 2014 at 7:13 Comment(1)
can you post your code?Vaginectomy
T
12

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.

Textuary answered 5/3, 2015 at 20:0 Comment(0)
N
3

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>
Neckerchief answered 21/5, 2015 at 9:22 Comment(0)
A
0

Try this :

ActionBar ab;
    ab = getActionBar();
    ab.setHomeButtonEnabled(true);
    ab.setTitle("Back");
    ab.setIcon(R.drawable.iconBack);

and ovrride function onOptionsItemSelected:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if(item.getItemId() == android.R.id.home)
            finish();
        return super.onOptionsItemSelected(item);
    }

Hope it helps!

Asphyxiate answered 12/12, 2014 at 7:27 Comment(1)
I am taking about contextual action bar and not the normal action bar of android.Vivie
M
0

This question is a bit old, but I decided to make an answer because I ran into the same problem today, and setting the activity theme wasn't a satisfactory answer for my use case. If you only need one specific close icon per an Activity, it's preferable to set actionModeCloseDrawable in the theme for the activity. However, if you need multiple close icons for action modes in the activity, it becomes necessary to use reflection to change the close ImageView's drawable:

/**
 * Attempts to change the ActionMode close icon drawable using reflection.
 *
 * @param drawableResId
 *      Resource ID of the new drawable
 *
 * @return true if the drawable was changed, false otherwise.
 */
private boolean setActionCloseDrawable(@Nullable final ActionMode actionMode, 
                                       final int drawableResId)
{
    if (actionMode != null)
    {
        // Change the drawable for the close ImageView to the desired icon
        // Unfortunately, this can only be done with reflection
        try
        {
            final Field wrappedObjectField = actionMode.getClass().getDeclaredField("mWrappedObject");
            wrappedObjectField.setAccessible(true);
            final Object mWrappedObject = wrappedObjectField.get(actionMode);

            final Field contextViewField = mWrappedObject.getClass().getDeclaredField("mContextView");
            contextViewField.setAccessible(true);
            final Object mContextView = contextViewField.get(mWrappedObject);

            final Field closeField = mContextView.getClass().getDeclaredField("mClose");
            closeField.setAccessible(true);
            final Object mClose = closeField.get(mContextView);

            if (mClose instanceof ImageView)
            {
                ((ImageView) mClose).setImageDrawable(getResources().getDrawable(drawableResId));
                return true;
            }
        }
        catch (Exception ex)
        {
            Log.e(TAG, ex.getClass().getSimpleName() + " in #setActionCloseDrawable: " +
                    ex.getLocalizedMessage() + '\n' + Log.getStackTraceString(ex));
        }
    }

    return false;
}
Marleen answered 13/10, 2019 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.