When selecting text in an android text view, a contextual action bar comes up with options to copy, cut, select all, share, etc. Is there a way to remove some of these options in my app?
You can inflate your own menu and then hide all the items that OS inserts.
First, keep track of all the IDs for your menu items:
List<Integer> mOptionsList = new ArrayList<Integer>();
/* put these two lines in onCreate() */
mOptionsList.add(R.id.my_option_1);
mOptionsList.add(R.id.my_option_2);
Then, hide any MenuItem that isn't yours in onPrepare:
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.my_contectual_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;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.my_option_1: {
/* do something for option 1 */
break;
}
case R.id.my_option_2: {
/* do something for option 2 */
break;
}
default:
return false;
}
}
public void onDestroyActionMode(ActionMode mode) {}
};
If you want to clear the default icons, You simply use menu.clear(). For instance altering the code above, we have
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear;
...
return true;
}
To remove a specific icon, you need to have the id of that icon. It would be something like menu.removeItem(android.R.id.copy) or something.
Related
I want to use contextual action bar in my app. When I start action mode, There are padding between default actionbar and contextual actionbar as shown in image 2. How can I avoid this padding ?
for start action mode
startSupportActionMode(modeCallBack);
callback method :
private ActionMode.Callback modeCallBack = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.driver_main_menu, 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_send:
Toast.makeText(getApplicationContext(), "Send!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
};
There are some questions like this already made in Stack Overflow but none of the selected solutions has worked for me. These links appear to make different choices, but I tried and I could not remove the first option which is by default.
Link 1 Link 2 Link 3 Link 4
Ignore the item icons.
I need to delete the item in the circle.
private class ModoAccion implements ActionMode.Callback {
private ArrayList<Integer> listaOpciones = new ArrayList<Integer>();
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
menu.clear();
inflater.inflate(R.menu.admin_detalle_emp_action_mode, menu);
actionMode.invalidate();
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
listaOpciones.add(R.id.accion_cancelar);
listaOpciones.add(R.id.accion_guardar);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (!listaOpciones.contains(item.getItemId()))
item.setVisible(false);
}
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.accion_cancelar:
actionMode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
mActionMode = null;
}
}
You can't actually get rid of that area of the context action bar. However, you can change its drawable (even to nothing if you like) by styling your app. Just override item name android:actionModeCloseDrawable and insert your own drawable.
I want to enable multiple view selection on longClick(). Should I declare an action mode object and call startActionMode()? Also, how would I change menu list for single item click? I used the documentation as reference, as shown.
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
To change menu list for single item click following is the code.
int count=0;
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (checked) {
count++;
} else {
count--;
}
mode.invalidate(); // Add this to Invalidate CAB so that it calls onPrepareActionMode
}
Now modify the onPrepareActionMode as follows
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selCount == 1){
//show the menu here that you want if only 1 item is selected
} else {
//show the menu here that you want if more than 1 item is selected
}
}
So my problem right now is that right now I am long clicking an item in a ListView which brings up a contextual action bar. The id passed into onItemLongClick is the variable that I would like to use in the mActionModeCallback's on ActionItemClicked() method. This seems like it would be a fairly common procedure since if a user is editing a list of items, you would want to access the id of that row in the database somehow when the user clicked an "edit" or a "delete" action.
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> p, View view, int pos, long id) {
//The id of the row in the database
long variableThatIWantToPassToCallback = id;
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//I would like access to the id of the clicked item here, NOT item.getItemId()
}
public void onDestroyActionMode(ActionMode mode) {}
};
The proper way to do this is to call mActionMode.setTag("1") in onItemCheckedStateChanged and then from the onActionItemClicked function call mode.getTag();
Create your own callback by extending the interface ActionMode.Callback
private interface ActionCallback extends ActionMode.Callback {
public void setClickedView(View view);
}
private ActionCallback mActionModeCallback = new ActionCallback() {
public View mClickedView;
public void setClickedView(View view) {
mClickedView = view;
}
// Called when the action mode is created; startActionMode() was called
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item ) {
switch ( item.getItemId() ) {
case R.id.menu_delete:
Log.v( TAG, "#onActionItemClicked ready to delete the item with id: " + mClickedView.getTag() );
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
} // end switch
}
// Called when the user exits the action mode
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
For a view which has OnLongClickListener attached, override the onLongClick callback this way.
#Override
// Called when the user long-clicks on someView
public boolean onLongClick( View view ) {
// proceed only when actionmode is not null
// otherwise overlapping action modes will be
// displayed
if( mActionMode != null ) {
return false;
}
mActionModeCallback.setClickedView(view);
// Start the CAB using the ActionMode.Callback defined above
mActionMode = startActionMode( mActionModeCallback );
view.setSelected(true);
return true;
}
In Android if the textview is selected then a word gets selected and a contextual action bar comes at the top...i want to modify that CAB and make it look like a quick action bar...keeping the text selection feature intact...please help me...
you can select textview or anything like only in simple layout CAB appear in the top and there was some method onActionItemClicke that you want to click an item and then you modify your action in CAB default behavior................................
public class MainActivity extends Activity {
protected Object mActionMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.lay);
view.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = MainActivity.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// Assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.cab, menu);
return true;
}
// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
Toast.makeText(MainActivity.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.shan:
Toast.makeText(MainActivity.this, "Selected shani",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
and in menu xml file create menu that you would like to appear in top CAB
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/toast"
android:title="Toast">
</item>
<item
android:id="#+id/shan"
android:title="shani">
</item>
</menu>