setIcon(ic_menu_more) doesn't have effect - android

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

Related

Multiselection mode doesn't end automatically when I click Delete

I'm working on a "small" example to learn about multiselect mode for a ListView. I set the mode on my list:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener(this));
}
Inflate the menu:
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = activity.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
And handle the menu event:
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()) {
case R.id.delete_menu:
activity.deleteSelectedWords();
return true;
}
return false;
}
Is there something else I need to do to end the action mode and return to the normal action bar?
Call clearChoices() on the ListView, as that should exit the action mode. Do this after you do your activity.deleteSelectedWords() bit. See this sample project for a complete working implementation.
Or, call finish() on the ActionMode itself.

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 to display option menu in android in Android API 17?

I am trying to display option menu. I am creating menu at run time using menu.add() method. I am using android API 17. IS there any menu button we have to click? I am having "menuItemsMap " as Map<> instance variable and i m adding menu into that map so that i can reuse these menu.
Thanks in Advance.
public boolean onCreateOptionsMenu(Menu menu) {
menuItemsMap = new HashMap<Integer, MenuItem>();
menuItemsMap.put(
R.string.pizzasCart_pizzasList,
menu.add(R.string.pizzasCart_pizzasList).setIcon(
R.drawable.script_edit));
menuItemsMap.put(
R.string.pizzasList_viewShoppingCart,
menu.add(R.string.pizzasList_viewShoppingCart).setIcon(
R.drawable.cart));
menuItemsMap.put(
R.string.pizzasCart_checkout,
menu.add(R.string.pizzasCart_checkout).setIcon(
R.drawable.cart_go));
menuItemsMap.put(
R.string.pizzasList_viewUserData,
menu.add(R.string.pizzasList_viewUserData).setIcon(
R.drawable.user_green));
/*menu.add(1,1,0,R.string.pizzasCart_pizzasList).setIcon(R.drawable.script_edit);
menu.add(1,2,1,R.string.pizzasList_viewShoppingCart).setIcon(R.drawable.cart);
menu.add(1,3,2,R.string.pizzasList_viewUserData).setIcon(R.drawable.user_green);*/
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
setMenuItemState(R.string.pizzasCart_pizzasList, false, false);
setMenuItemState(R.string.pizzasList_viewShoppingCart, true,
!isShoppingCartEmpty());
setMenuItemState(R.string.pizzasCart_checkout, true,
isShoppingCartCheckoutAllowed());
setMenuItemState(R.string.pizzasList_viewUserData, true, true);
return true;
}
protected void setMenuItemState(int itemTitleResID, boolean visible, boolean enabled) {
MenuItem item = menuItemsMap.get(itemTitleResID);
item.setEnabled(enabled);
item.setVisible(visible);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals(getString(R.string.pizzasList_viewUserData))) {
showUserDataActivity();
} else if (item.getTitle().equals(
getString(R.string.pizzasCart_pizzasList))) {
showPizzasListActivity();
} else if (item.getTitle().equals(
getString(R.string.pizzasList_viewShoppingCart))) {
showPizzasCartListActivity();
} else if (item.getTitle().equals(
getString(R.string.pizzasCart_checkout))) {
checkoutShoppingCartPromptUser();
}
return true;
}
the menu button is the three dots on the top right of the action bar #4
read up on menus http://developer.android.com/guide/topics/ui/menus.html

How can I add an Action Bar Item during run time

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

Android::menu cannot be resolved or is not a field

#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings1, menu);
return true;
}
I need help. i got error at "R.menu" which the error message "menu cannot be resolved or is not a field"..Thanks
If there is error in your layout the R file is not generated so it is normal to get this error.
You need to correct the errors in your layout first, then cleaning, and R will be resolved.
you are using Preference xml as an menu reference that's why reciving this error so make a menu.xml or add as:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
and you can add dynamically as:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "??").setIcon(
android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "??").setIcon(
android.R.drawable.ic_menu_edit);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
startActivity(new Intent(getBaseContext(), SettingActivity.class));
break;
case Menu.FIRST + 2:
Toast.makeText(getBaseContext(), "Menu Clicked", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
Check your imports. If you imported some kind of .R file from some other project (maybe a library project) like import com.someotherpackage.R; you need to delete that line, then clean your project.
Right now it's probably referencing the wrong .R file, or you have a typo in a name somewhere.

Categories

Resources