Multiselection mode doesn't end automatically when I click Delete - android

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.

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

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

android-seach option in actionBar without creating custom theme

Is it possible to put a search option on the right of the action bar without having to create a custom action bar ?
Follow this code If you are action bar sherlock, you can implement search menu in action bar
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Search")
.setIcon(R.drawable.search)
.setActionView(R.layout.search_layout)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_COLLAPSE_VIEW);
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//What do i write here?
return true;
}
In search_layout.xml
place a edittext that's enough.
Normal Android Actionbar search widget Implementation

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

Categories

Resources