How do I programmatically change ActionBar menuitem text colour? - android

I have an actionBar with multiple items, I would like to change the colour of the text when the item is clicked. Is there anyway to do this programmatically? Please provide and example or any resources.
Thanks
public void catalogClick(MenuItem item){
//highlight menuitem etc.
}

To change without defining a style resource, we can use SpannableString.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//To style first menu item
MenuItem menuItem = menu.getItem(0);
CharSequence menuTitle = menuItem.getTitle();
SpannableString styledMenuTitle = new SpannableString(menuTitle);
styledMenuTitle.setSpan(new ForegroundColorSpan(Color.parseColor("#00FFBB")), 0, menuTitle.length(), 0);
menuItem.setTitle(styledMenuTitle);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Toast.makeText(this, item.getTitle() + " clicked!", Toast.LENGTH_LONG).show();
return true;
}
As you format the text style, you will get "Invalid payload item type" exception. To avoid that, override onMenuItemSelected, and use return true or false.
Reference:
Android: java.lang.IllegalArgumentException: Invalid payload item type
http://vardhan-justlikethat.blogspot.in/2013/02/solution-invalid-payload-item-type.html

Follow this link which explains how to change menuitem text programmatically.
http://developer.android.com/guide/topics/ui/actionbar.html#Style
Check for android:actionMenuTextColor for defining a style resource for text.

Try Firewall_Sudhan answer but iterating the submenu of the menu
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SubMenu subMenu = menu.getItem(0).getSubMenu();
for (int i = 0; i < subMenu.size(); i++) {
MenuItem menuItem = subMenu.getItem(i);
CharSequence menuTitle = menuItem.getTitle();
SpannableString styledMenuTitle = new SpannableString(menuTitle);
styledMenuTitle.setSpan(new ForegroundColorSpan(Color.BLACK), 0, menuTitle.length(), 0);
menuItem.setTitle(styledMenuTitle);
}
}

Related

Show popup menu when action bar item is clicked

How to add a menu popup when an action bar item is clicked (see screenshot)? I want the menu item to show an icon.
tHings I have tried:
Setting actionProvider (support lib v7) for the action bar item. In the actionProvider, return null for onCreateActionView. In onPrepareSubMenu, populate the submenu. This works on Android 2.x but not Android 4.0, and for Android 2.x, there is no icon.
In the actionProvider, create a imageview and on clicking, shows a PopupMenu, but popup menu has no icon, when I have specifically used setIcon to show it.
I don't understand why PopupMenu does not show any icon. I followed the "official" code as closely as possible but to no avail.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/widget/ShareActionProvider.java#195
Please help!
Thanks!
Use popUpMenu ->>> Follow>
res/menu/horario.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_MudaDia"
android:titleCondensed="Mudar Dia"
android:title="Mudar Dia"
android:icon="#drawable/ic_menu_popup"
android:showAsAction="always">
</item>
activity.class
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.horario, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_MudaDia:
View vItem = getActivity().findViewById(R.id.menu_MudaDia);
PopupMenu popMenu = new PopupMenu(getActivity(), vItem);
for (int i = 0; i < diaSemana.length; i++)
{
popMenu.getMenu().add(0, i, i, diaSemana[i]);
}
popMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
DIA = diaSemana[item.getItemId()];
atualizaGUI();
return true;
}
});
popMenu.show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
You can try creating a layout with the ImageView and TextView. Inflate that layout inside a PopUpWindow (refer : http://developer.android.com/reference/android/widget/PopupWindow.html).
Use the showAsDropDown(View actionBarIcon) method to show the menu on your actionbar icon click.
I use support library v7 and works well.
- use ActionProvider
I use custom ActionProvider and it works well at 2.x and 4.x ,
code in onPrepareSubMenu
subMenu.clear();
// labels contain list item text.
int len = labels.length;
for(int i = 0; i < len; i++) {
subMenu.add(0, labels[i], i, labels[i])
.setIcon(icons[i])
.setOnMenuItemClickListener(new MineMenuItemClickListener());
}
super.onPrepareSubMenu(subMenu);
- about PopupMenu
PopupMenu don't show icon as default, but you can do your own PopupMenu and set icon display.
Like this man does CustomPopupMenu
The only modify is add mPopup.setForceShowIcon(true);

Android Options Menu Without ActionBar?

what I'm looking for is to make an options menu but without the ActionBar. In the Google music app I saw that they have a options menu sort of thing with no action bar. Below is a picture of what I was talking about in the Google music app.
Thank you in advance! :)
That's just a simple popop. You can do that on any view. Throw an icon on a view, like the overflow menu icone and set a click listener on it.
This example is a list of devices (smartphones) in a catalog. I populate the tag with an object so I know which one the user clicks on.
public void showDeviceMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.inflate(R.menu.cart_device_menu);
DeviceTag tag = (DeviceTag) v.getTag();
final String groupId = tag.groupId;
final String sku = tag.sku;
final String productId = tag.productId;
SpannableStringBuilder text = new SpannableStringBuilder(tag.name);
text.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
popup.getMenu().findItem(R.id.menu_name).setTitle(text);
invalidateOptionsMenu();
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.duplicate_device:
duplicateDevice(sku, productId);
return true;
case R.id.update_device:
updateWirelessItemInCart(sku,groupId);
return true;
case R.id.delete_device:
removeItemFromCart(groupId);
return true;
default:
return false;
}
}
});
popup.show();
}

How to add more options to Android default Contextual Action Bar

As we know, by default, after selecting some text on views, android displays Contextual Action Bar (CAB) with some default options, such as: copy, cut, select all...
Now, I want to have an application (that has only 2 options: ON/OFF), If I turn it ON, Some other options will be added to default CAB. If I turn it OFF, my custom options will be removed from Android default CAB.
My question is: Is it possible to Add/Remove some options to this default CAB? How can I make above application?
Thank you!
You'll have to use the setCustomSelectionActionModeCallback on each of your TextViews.
You can have a boolean:
boolean on = true;
Then create a method that actually edits the CAB like so:
private void editContextualActionBar(ActionMode actionMode, Menu menu) {
if (on) {
// adds a new menu item to the CAB
// add(int groupId, int itemId, int order, int titleRes)
menu.add(0, R.id.action_to_be_performed, 1, R.string.action_name);
} else {
// removes the new menu item
menu.removeItem(R.id.action_to_be_performed);
}
}
Finally, call the Callback on your TextView with the editContextualActionBar method in onCreateActionMode and perform the menu action in onActionItemClicked:
textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
editContextualActionBar(mode, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_to_be_performed:
// perform action
return true;
default:
break;
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});

How do i set my menu buttons location

So what i want to do, is to have that menu button that we all know, to be displayed in the top right corner.
And as I've searched online, I found that actionbar Sherlock kind of suites my needs.
As you can see from he pic, the code that I use, adds that darker blue part in my design (the one that I ilustrated in a red rectangle using my awesome paint skills), and i want that button that is a green circle, to be placed in the area where the green arrow (nice movie :D) points.
The only code that i used to display this is below, and it is taken from the Sherlock demos:
ActionMode mMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMode = startActionMode(new AnActionModeOfEpicProportions());
}
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//Used to put dark icons on light action bar
// boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
menu.add("Feedback")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add("Share")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add("Preferences")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add("Refresh")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(AndroidMenu.this, "Got click: " + item, Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
I can't seem to figure out where that design comes from (probably the Sherlock framework), and how can i modify it like I want...
Any ideas guys ?
EDIT:
The answer that I found is to use a different Sherlock Sample for my goal :
public class SubMenus extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Action Item");
subMenu1.add("Sample");
subMenu1.add("Menu");
subMenu1.add("Items");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_title_share_default);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu subMenu2 = menu.addSubMenu("Overflow Item");
subMenu2.add("These");
subMenu2.add("Are");
subMenu2.add("Sample");
subMenu2.add("Items");
MenuItem subMenu2Item = subMenu2.getItem();
subMenu2Item.setIcon(R.drawable.ic_compose);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
((TextView)findViewById(R.id.text)).setText(R.string.submenus_content);
}
The best way to personalize such objects is to set a custom View on them. Perhaps you can inflate a custom layout which provides one TextView and a Button on the right, without the "Done" mark on the left.
This is a snippet of code from this
LayoutInflater inflater = LayoutInflater.from(getSherlockActivity());
View actionView = inflater.inflate(R.layout.actionmode, null);
ActionMode am = getSherlockActivity().startActionMode(mActionModeCallback);
am.setCustomView(actionView);

Android - Can't bold more than a word in edittext

I have an editText in which i want to bold the text that i select.
I'm using the Contextual Action Bar with a button to bold a selected word.
The problem is that if I bold a word I can't bold the other onesm and if i remove the span from that word, I can't add it again.
et is the editText in which i write, and i use also 2 SpannableString to catch the remaining text keeping eventual spans added before on it.
CUSTOM CALLBACK
cs1 = new StyleSpan(Typeface.BOLD);
class CustomCallback implements ActionMode.Callback {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//exploiting the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
menu.removeItem(android.R.id.selectAll);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
ACTION ADD BOLD
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
int start = et.getSelectionStart();
int end = et.getSelectionEnd();
SpannableStringBuilder s_before,s_next;
SpannableStringBuilder ssb = new SpannableStringBuilder(et.getText().subSequence(start, end));
s_before= new SpannableStringBuilder (et.getText().subSequence(0, start));
s_next= new SpannableStringBuilder (et.getText().subSequence(end, et.length()));
switch(item.getItemId()) {
case R.id.bold:
int a=ssb.getSpanStart(cs1);
int b=ssb.getSpanEnd(cs1);
if(a==-1 && b==-1){
ssb.setSpan(cs1, 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else{
ssb.removeSpan(cs1);
}
et.setText("");
et.append(s_before);
et.append(ssb);
et.append(s_next);
return true;
How can i solve it?
Thanks in advance.
I've just put a RichEditText library https://github.com/kemallette/RichEditText which adds bold/italic/strike/underline.. and a few other font styles functionalities. It also adds a bunch of great validations.
If that doesn't fit your needs, you'll need to focus on keeping track of where your spans are, what type they are and that they're being adjusted/re-added. Take a look at the RichEditText and RichTextWatcher classes in the above library. It'll give you a better idea of what's actually happening when text changes in your EditText.

Categories

Resources