How to display option menu in android in Android API 17? - android

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

Related

Remove CAB but preserve webview text selection

I want to implement my own custom view (not inflating a menu item), I'm planning to use a toolbar to appear each time contextMenu starts, and hide it when finished.
the problem is: there are only answer showing HOW to clear/inflate another menu over the default actionMode menu
what i`ve tried so far:
-> Use a custom contextual action bar for WebView text selection
Overriding the callback at the WebView
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
callback2 = new customCallBack();
return super.startActionMode(callback2);
}
public class customCallBack implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
changing the return false to true, doesn't result in the desired behavior i.e. hide the cab
Overriding the OnLongClick is not a option too, since it disable the current selection.
This answer solves the problem:
android webview: prevent text selection actionMode actionBar
not the most elegant solution ever, but I just tested it in an app I'm building and it works like a charm.
The Only way that worked for me (only on on Android L+) is clearing all the menu items from context actionbar from the activity
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
// Remove the default menu items (select all, copy, paste, search)
menu.clear();
}
Toast.makeText(this, "onActionModeStarted", Toast.LENGTH_SHORT).show();
super.onActionModeStarted(mode);
}
#Override
public void onActionModeFinished(ActionMode mode) {
mActionMode = null;
Toast.makeText(this, "onActionModeFinished", Toast.LENGTH_SHORT).show();
super.onActionModeFinished(mode);
}
inspired by Use a custom contextual action bar for WebView text selection
Also I wasn't able to implement the custom menu usin popupWindow or dialogs or dialog fragments.
So simply put it with the webView in a frame layout and play with its visability and margin

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 start an action mode in android 3.0

We have this default feature in android 3.0 that when we long press somewhere (say in a webview) action mode is started and as a result the action bar changes with many default features like copy, find etc.
Now what I want is that I want to start this action mode but not by long pressing on the web view. I want that when I load a webview I want this action mode automatically started without any long press. Is it possible ? Is there any method by which we can achieve this?
In case you still need it or other people are looking for it
startActionMode(mContentSelectionActionModeCallback);
And as callback use:
private ActionMode.Callback mContentSelectionActionModeCallback =
new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
//Here you can inflate a menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.name_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
return false;
}
public void onDestroyActionMode(ActionMode actionMode) {
awesomeAdapter.overviewFragment.stopEdit();
}
};

option item selection in Dialog menu

I have been running into some troubles recently and I think I need your help :).
I am currently trying to show a menu on top of a dialog, I know that it could be far easier to launch a new activity yet doing so would compell me to store/pass a lot of data.
I managed to show an optionmenu by writing a custom dialog and rewriting the oncreateOptionMenu method.
My problem is I can't get any listener to these button, I tried to rewrite the onoptionitemselectedmethod but nothing happens.
Ps: my dialog is nearly full screen so i can't see the activity dialog (i didn't find any put on top method)
I would be glad to try any solution you could provide.
Thanks a lot
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==0){
getOwnerActivity().startActivity(new Intent(c,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else
if(l>1)
getOwnerActivity().startActivity(new Intent(c,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
else
dismiss();
return true;
}
Maybe this little snippet out of my app helps you:
private static final int REFRESH_ID = Menu.FIRST + 1;
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, REFRESH_ID, 0, R.string.menu_refresh).setIcon(R.drawable.and_refresh);
return result;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case REFRESH_ID: {
// Do whatever you want here!
return true;
}
}
return super.onMenuItemSelected(featureId, item);
}
As you can see, I've got constants for my menu-items. Those items get the Menu.First + n number as integer. For every item, I count it up. Easier, then change it everytime ;)
And in the onMenuItemSelected you can switch those constants easily.
Hope that helps!

Categories

Resources