Android EditText without QuickAction - android

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

Related

How to Disable Only Cut option From EditText?

i have two EditText for UserName and Password. first UserName EditText will be Visible.it will check Valid UserName.Then, Password EditText will be visible. i want user to copy the username but don't want to cut.
how to disable the Cut Option.
But below code Disable all the Option.
Code :
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
First set a customSelectionActionModeCallback.
You could identify the cut option by its id: android.R.id.cut
So your code in the onCreateActionMode from the ActionMode.Callback would look like this:
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuItem menuItem = menu.findItem(android.R.id.cut);
if (menuItem != null) {
menuItem.setVisible(false);
}
return true;
}
Or you could even remove it by using menu.removeItem(android.R.id.cut).
Also null-check for menu.findItem(android.R.id.cut) because it might return null when calling setVisible on it.

setCustomSelectionActionModeCallback not work with my editText :(

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

How to disable copy paste in android

I am working in xamarin forms to create an app for android. I want to disable the copy/paste functionality of textbox in android. I used the following line to disable it
Control.LongClickable = false;
But its working only in case of if user press the text for long time. But if user click multiple times on text, he becomes able to copy paste. How I can completely disable the copy paste functionality of textbox?
In eclipse this can be used, Just check will it be helpful.
textView.setCustomSelectionActionModeCallback(new ActionMode.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;
}
});
// after onCreateView and findById you you_editText view;
you_editText.CustomSelectionActionModeCallback = new CopyPasteDisabler();
class CopyPasteDisabler : Java.Lang.Object, Android.Views.ActionMode.ICallback
{
public bool OnActionItemClicked(Android.Views.ActionMode mode, IMenuItem item) => false;
public bool OnCreateActionMode(Android.Views.ActionMode mode, IMenu menu) => false;
public void OnDestroyActionMode(Android.Views.ActionMode mode) {}
public bool OnPrepareActionMode(Android.Views.ActionMode mode, IMenu menu) => false;
}

How to disable copy/paste from/to SearchView

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

how to disable paste option in android EditText

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

Categories

Resources