In edittext long press created contextmenu. How to remove the contextmenu?
I tried this:
et.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
et.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
return false;
}
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode actionMode,
MenuItem item) {
return false;
}
public void onDestroyActionMode(ActionMode actionMode) {
}
});
But then I can not select text.
I'm not sure that you can remove the menu altogether, but you can call menu.clear() in onPrepareActionMode and this will remove all the menu items from the menu. You'll have to remove the onLongClickListener in order to see the handles again as well.
You can create your own class extending EditText and override the performLongClick() method, that should work as expected
class MyET extends EditText{
public MyET(Context context) {
super(context);
}
#Override
public boolean performLongClick() {
// TODO Auto-generated method stub
return true;
}
}
Related
On my editText, when i do setCustomSelectionActionModeCallback(new Callback() {}) then the Callback is never called :( i think everything on the edittext is well configured, i do
settextIsSelectable(true);
setfocusable(true);
setfocusableInTouchMode(true);
setlongClickable(true);
What in my editText can forbid the callback ? how to build custom context menu for my edittext ?
This is my code :
this.setCustomSelectionActionModeCallback(new Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Log.e("onPrepareActionMode","onPrepareActionMode");
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Log.e("onCreateActionMode","onCreateActionMode");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
Log.e("onDestroyActionMode","onDestroyActionMode");
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.e("onActionItemClicked","onActionItemClicked");
return true;
}
});
I would like to disable copy/paste actions when I use the SearchView in Toolbar.
Actually I don't want to have the native copy/past bar which appears on top.
You can use this method:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void disableSearchViewActionMode(SearchView searchView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
((EditText) searchView.findViewById(R.id.search_src_text)).setCustomSelectionActionModeCallback(new 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) {
}
});
}
}
I've googled a lot, and find some tutorials and answers here in stackoverflow, but I am facing some difficults to resolve this issue.
I have a Fragment with a WebView, and I want to show my custom Contextual Action Bar when the user selects some text of my web view. I have two main issues here:
1 Currently, my custom CAB is showed when the user performs long click in any part of the web view.
2 The text is not selected when the user performs long click in some text of my web view.
Some of my current code:
Custom interface:
public class SelectActionModeCallback 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) {
}
}
Custom WebView
public class CustomWebView extends WebView {
private SelectActionModeCallback actionModeCallback;
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public ActionMode startActionMode(Callback callback) {
actionModeCallback = new SelectActionModeCallback();
return super.startActionMode(actionModeCallback);
}
In my fragment, I have this:
#Override
public void onResume() {
super.onResume();
myWebView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (mActionMode != null) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_SHORT).show();
return false;
}
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
private SelectActionModeCallback mActionModeCallback = new SelectActionModeCallback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.custom, 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_sustom:
customMethod();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
THERE IS A BETTER WAY! See the following answer: https://stackoverflow.com/a/22391169/2608235
In short, you can't use an OnLongClickListener and keep the selection within a WebView. Android does some weird stuff behind the scenes for selecting text within WebViews. If you override OnLongClickListener in order to call startActionMode, you will lose the selection, as you have found out.
What you should do instead is override startActionMode in your fragment, rather than its parent View (in your case, CustomWebView).
I don't have the mod permission to mark this question as a duplicate, but it is.
See my question for more info: Use a custom contextual action bar for WebView text selection
I have a requirement where EditText doesn't allow paste but it should allow copy.
I tried setCustomSelectionActionModeCallback but it disables the copy option.
EditText etxt = (EditText) findViewById(R.id.editText1);
etxt.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
I did it myself..credit goes to Android intercept paste\copy\cut on editText
I just changed onTextContextMenuItem() based on my requirement..
#Override
public boolean onTextContextMenuItem(int id) {
// Do your thing:
//boolean consumed = super.onTextContextMenuItem(id); // Change1
// React:
switch (id){
case android.R.id.cut:
onTextCut();
break;
case android.R.id.paste:
onTextPaste();
return false; //Change2
//break;
case android.R.id.copy:
onTextCopy();
}
return true; // Change3
}
If you are using API level 11 or above use this
edittext.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
If you want to disable only the paste option, I think it is better to remove the paste context menu item, this is a util method I implemented in my apps to achieve that (I also removed cut item)
public static void toggleContextMenuType(EditText editText) {
if (editText.getCustomSelectionActionModeCallback() == null) {
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.removeItem(16908322);
menu.removeItem(16908320);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}else{
editText.setCustomSelectionActionModeCallback(null);
}
}
My problem is, when I select some text in an EditText (version 4.0.3), a QuickAction (showing several options like cut, copy, paste) will appear near the selected text. I Don't need this popup, so how can I turn it off or something like that? Thanks
edittext.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
public void onDestroyActionMode(ActionMode mode) { }
public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; } public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; } });
you can add callback for your editText like this :
YOUR_EDITTEXT_OBJECT.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});