Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock (ABS): how to customize the text of action mode close item?

I'm using ABS vers. 4 and I need to simply change the default "Done" text that is displayed besides the action mode close icon, but I really can't figure out how to do it.

I think that text needs to be customizable for at least two good reasons:

  • "Done" is not appropriate for all contexts (e.g. "Cancel" could be more appropriate, and I've seen some apps, such as the "My Files" app on Galaxy Tab, use it)

  • "Done" needs to be localized according to the user's language

Is it possible to do customize that text? If so can anyone tell me how to do it?

Thanks in advance.

EDIT

I've found a temporary workaround, that I post in the following:

private TextView getActionModeCloseTextView() {
    // ABS 4.0 defines action mode close button text only for "large" layouts
    if ((getResources().getConfiguration().screenLayout & 
        Configuration.SCREENLAYOUT_SIZE_MASK) == 
        Configuration.SCREENLAYOUT_SIZE_LARGE) 
    {
        // retrieves the LinearLayout containing the action mode close button text
        LinearLayout action_mode_close_button =
            (LinearLayout) getActivity().findViewById(R.id.abs__action_mode_close_button);
        // if found, returns its last child 
        // (in ABS 4.0 there is no other way to refer to it, 
        // since it doesn't have an id nor a tag)
        if (action_mode_close_button != null) return (TextView)
            action_mode_close_button.getChildAt(action_mode_close_button.getChildCount() - 1);
    }
    return null;
}

That's the method I came up with. Please NOTE that it does heavily rely upon the structure of the abs__action_mode_close_item.xml of ABS 4.0.

This works for my scenario, but, as you can see, it cannot be considered sufficiently satisfying to promote it to a real "answer", that's why I only edited my previous post.

Hope that helps someone else, but I also hope that someone could share a better and cleaner solution.

like image 313
Luke Avatar asked Apr 05 '12 10:04

Luke


3 Answers

You can use a theme to override the default icon:

    <item name="actionModeCloseDrawable">@drawable/navigation_back</item>
    <item name="android:actionModeCloseDrawable">@drawable/navigation_back</item>
like image 178
Yichuan Wang Avatar answered Oct 10 '22 07:10

Yichuan Wang


I edited the code from PacificSky to be able to customize the color and font size of the close button, both in pre ICS and >ICS.

I created a method named customizeActionModeCloseButton

private void customizeActionModeCloseButton() {
      int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");    
      View v = getGSActivity().findViewById(buttonId);
      if (v == null) {
         buttonId = R.id.abs__action_mode_close_button;
         v = getGSActivity().findViewById(buttonId);
      }
      if (v == null)
         return;
      LinearLayout ll = (LinearLayout) v;
      if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
         TextView tv = (TextView) ll.getChildAt(1);
         tv.setText(R.string.close_action_mode);
         tv.setTextColor(getResources().getColor(R.color.white));
         tv.setTextSize(18);
      }
   }

and I call it just after calling startActionMode()

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
   actionMode = getActivity().startActionMode(this);
   customizeActionModeCloseButton();
   return true;
}
like image 7
guillaume-tgl Avatar answered Oct 10 '22 07:10

guillaume-tgl


It's been a while, but here's a slightly less hacky solution - putting it out there for posterity.

For Android versions < ICS

Put the following line in your application's strings.xml:

<string name="abs__action_mode_done">Cancel</string>

This overrides the TextView's (defined in ActionBarSherlock/res/layout-large/abs__action_mode_close_item.xml) android:text attribute.

For Android versions ICS and above

The native ActionBar functionality is used on ICS and up. You need to find and override the string associated with the done button, using the following code:

int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
if (buttonId != 0)
{
    View v = findViewById(buttonId);
    if (v != null)
    {
        LinearLayout ll = (LinearLayout)v;
        View child = ll.getChildAt(1);
        if (child != null)
        {
            TextView tv = (TextView)child;
            tv.setText(R.string.cancel);
        }
    }
}
like image 6
PacificSky Avatar answered Oct 10 '22 08:10

PacificSky