How can I add an Action Bar Item during run time - android

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

Related

Selecting overflow menu item on TextView CustomActionModeCallback

I am trying to present a custom action bar while long pressing a textview. My menu has more than 5 items which causes some of the items to be present under the overflow menu.
When I press the overflow icon, the action bar gets destroyed and I am not able to choose any item inside the overflow.
ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.add_rule_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (!mOptionsList.contains(item.getItemId()))
item.setVisible(false);
}
return false;
}
// Clicking on overflow button does not trigger this method at all.
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
// Rest of the code
}
}
public void onDestroyActionMode(ActionMode mode) {}
};
textView.setCustomSelectionActionModeCallback(mActionModeCallback);
I filed an issue about this years ago, which has never been resolved.
A cheesy workaround is to use nested action modes. By this, I mean you have an item in an action mode that finishes the current mode and starts a new one, to provide a "drill-down menu" effect. I use this in my recently-resuscitated RichEditText widget, which offers an action mode for formatting text. I add a "format" item to the default action mode via setCustomSelectionActionModeCallback(). Tapping on "format" opens another action mode that offers options like bold and italic, along with further drill-downs to get to thinks like font changes.

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) {
}
});

android change option menu text color

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/

Changing options menu/actionbar menu programatically for 3.0+

In short, here's my question:
Can option menus (shown in the actionbar) be modified programatically on android 3.0+?
I have a wizard-style activity in which I use a ViewFlipper to switch between views, or steps.
The steps are: 1 -> 2 -> 3. Only the second screen (2) has a menu item, while the others don't. I have tried hanging on to the Menu reference (source) and either removing/adding items or just hiding/showing them.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
mMenu.getItem(0).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Switch to the second screen ->
public void showNext(View v) {
if (mVFlipper.getDisplayedChild() < (mVFlipper.getChildCount() - 1)) {
mVFlipper.showNext();
if (mVFlipper.getDisplayedChild() == 1) {
setTitle("Second screen");
mMenu.getItem(0).setVisible(true);
}
}
}
This works fine on 2.2, but fails miserably on 4.1. Starting off with a visible MenuItem and hiding it later works. Starting off with an invisible menu item and showing it later -
There is a bug in Android's MenuItem setVisible that causes problems when turning items back to visible.
In your onCreateOptionsMenu(), add a check to see if the displayed page needs the Menu, if it does, add the MenuItem. Then, call invalidateOptionsMenu() whenever the page changes. That will rebuild the Menu.
I was also struggling with this issue, then I applied a small hack:
menu1.setEnabled(false);
menu1.setTitle("");
Then where you want to visible it again:
menu1.setEnabled(true);
menu1.setTitle("Okay"); //or you can set text according to your given updated values.
Problem fixed by having the MenuItem be visible after onCreateOptionsMenu finishes and then hiding it from a callback called after onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
boolean dummyVal = super.onCreateOptionsMenu(menu);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
mMenu.getItem(0).setVisible(true);
} else {
mMenu.getItem(0).setVisible(false);
}
return dummyVal;
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (mMenu != null) {
mMenu.getItem(0).setVisible(false);
}
}
}
If anyone has this problem, I recommend trying toadzky's suggestion first: calling "invalidateOptionsMenu()".

setIcon(ic_menu_more) doesn't have effect

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);
}

Categories

Resources