Actionbarsherlock (abs): How To Customize The Text Of Action Mode Close Item?
Solution 1:
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>
Solution 2:
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
privatevoidcustomizeActionModeCloseButton() {
intbuttonId= Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
Viewv= getGSActivity().findViewById(buttonId);
if (v == null) {
buttonId = R.id.abs__action_mode_close_button;
v = getGSActivity().findViewById(buttonId);
}
if (v == null)
return;
LinearLayoutll= (LinearLayout) v;
if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
TextViewtv= (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()
publicbooleanonItemLongClick(AdapterView<?> parent, View view, int position, long id) {
actionMode = getActivity().startActionMode(this);
customizeActionModeCloseButton();
returntrue;
}
Solution 3:
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:
intbuttonId= Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
if (buttonId != 0)
{
Viewv= findViewById(buttonId);
if (v != null)
{
LinearLayoutll= (LinearLayout)v;
Viewchild= ll.getChildAt(1);
if (child != null)
{
TextViewtv= (TextView)child;
tv.setText(R.string.cancel);
}
}
}
Solution 4:
Thanks for PacificSky's answer. It's useful for my case.
Something needs to be explained here is that findViewById(buttonId)
might return null
in some cases such as called in onCreateActionMode()
function, because the LinearLayout
for ActionMode close button not yet initialized at that time I guess.
I want to hide the action mode close button, so i just sendEmptyMessageDelayed
in onCreateActionMode()
and call PacificSky's 200ms later. It works for me.
Solution 5:
Here is my approach with Java code:
privatevoidcustomizeActionModeCloseButton(String title, int iconID) {
intbuttonId= Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
Viewv= findViewById(buttonId);
if (v == null) {
buttonId = R.id.abs__action_mode_close_button;
v = findViewById(buttonId);
}
if (v == null)
return;
LinearLayoutll= (LinearLayout) v;
if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
//custom iconImageViewimg= (ImageView) ll.getChildAt(0);
img.setImageResource(iconID);
//custom textTextViewtv= (TextView) ll.getChildAt(1);
tv.setText(title);
tv.setTextColor(Color.WHITE);
}
}
Post a Comment for "Actionbarsherlock (abs): How To Customize The Text Of Action Mode Close Item?"