I have been developing app these days, the code for changing text items:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
int positionOfMenuItem = 0; // or whatever...
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitleCondensed(s);
return true;
}
it works pretty, but when I clicking the menu item it stops with error
Use one of the Spannable flag constants as the last argument to the call to setSpan(), e.g. Spannable.SPAN_EXCLUSIVE_EXCLUSIVE. See http://developer.android.com/reference/android/text/Spannable.html.
Also, consider moving the code after inflate to onPrepareOptionsMenu().
The following post might be useful: http://www.techrepublic.com/article/style-androids-overflow-menu-using-this-sanity-saving-workaround/
Related
I would like to disable the ActionBar Menu Item but make it clickable. Is there any way I can do this?
Right now, it's just greyed out if I set setEnabled as false which is normal behavior but it's not clickable.
This doesn't really answer the question as the ActionBar Menu Item cannot be disabled and clickable at the same time. What I ended up doing was enabling the Menu Item and setting the text color to grey to make it look pseudo-disabled. And if the condition was fulfilled it would change to default color.
Credit: Nicolai Buch Andersen
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.menu_title));
if (someCondition){
text.setSpan(new ForegroundColorSpan(Color.BLACK),
0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else {
text.setSpan(new ForegroundColorSpan(Color.LTGRAY),
0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
MenuItem item = menu.findItem(R.id.some_menu_id);
item.setTitle(text);
return true;
}
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.
How can I add an Action Bar Item during run time?
I am using actionBarSherlock, and I need to add some buttons when an event occurs (get some texts from a RSS, for example). I can't rely on a fixed xml.
You can create the menu in code like this:
/*************************************/
/* Create the actionbar options menu */
/*************************************/
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, "History").setIcon(R.drawable.ic_menu_recent_history)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(0, 1, 0, "Settings").setIcon(R.drawable.ic_menu_manage)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Inside check for a boolean.
You will need to call supportInvalidateOptionsMenu() to recreate the menu.
You can maintain a flag that determines if you need to display your button
boolean hasRss = false;
then, override the method onCreateOptionsMenu(Menu menu) and check to see if hasRss is true or false. If true, add your button to do whatever. Then you can add your normal buttons you want to always show up regardless if you have the RSS or not
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
if (hasRss)
{
menu.add(Menu.NONE, 0, Menu.NONE, "View RSS").setIcon(R.drawable.ic_menu_view)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
menu.add(Menu.NONE, 1, Menu.NONE, "Normal button that is always there").setIcon(R.drawable.ic_menu_button)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
you will have to set your hasRss value = true whenever you retrieve your values and call invalidateOptionsMenu(); to reload the action bar menu items
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);
}
}
Sample code from the book:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem menuItem = menu.add(0, Menu.FIRST, 0, "Go");
menuItem.setIcon(android.R.drawable.ic_menu_more); // doesn't work
return true;
}
When I press Menu button in Android emulator, "Go" option is shown in the bottom of the screen, but without any icon - both if setIcon called or not. What is wrong?
refer this tuto . hope it helps
EDIT :
try this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu m = menu.addSubMenu(0, 1000, 0, "Go");
m.setIcon(android.R.drawable.ic_menu_add);
return super.onCreateOptionsMenu(menu);
}