Disable Paste option in edittext when tapping cursor - android

I have set CustomSelectionActionModeCallback to an edittext as below
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) {
}
};
to disable paste option on edittext,but still user can paste by tapping on the cursor .I tried to create custom edittext class and override canPaste(),but still no luck.Can anybody suggest a correct solution.
Thanks.
Apparently the problem is observed in lollipop+ devices only

Another method one would suggest is to call setLongClickable(false) on the EditText, although that still doesn't prevent pasting from tapping on the cursor.
The proper (or only :) way to do this is to create a custom EditText and override isSuggestionsEnabled and canPaste methods.
Check this answer for an example.

Related

How to disable "share" feature in Android EditText

Whenever I have text in an EditText, the user can do a longpress over the text and the "Copy/Cut/Share" options hover over it.
It is OK for me to let Copy/Cut (Paste also) options appear (although not really needed), but I really can't let those text trigger the "share" option appear.
Is this feasible without reinventing the wheel ?
The XML code to define the EditText is quite basic, currently testing against API 30:
<EditText
android:id="#+id/settings_doctor_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/dr_name"
android:inputType="textCapCharacters"
android:singleLine="true"
android:textSize="26sp" />
Having had a review on EditText and TextView documentation, but I still haven't found any idea on do to deal with this.
Did I miss anything in the docs ? Thanks for reading !
I share with you the solution derived from your comments that has been implemented and tested:
I created a my own TextEdit:
public class EditText extends android.support.v7.widget.AppCompatEditText {
public EditText(Context context) {
super(context);
this.setCustomSelectionActionModeCallback(removeShareActionMode);
}
ActionMode.Callback removeShareActionMode = new ActionMode.Callback() {
#Override
#TargetApi(26)
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (menu != null) {
menu.removeItem(android.R.id.shareText);
}
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
}
And used it everywhere. Works as expected. Thanks to all for contributing. I'm sorry I don't have enough reputation to vote your answers.

How to hide ActionPopupWindow of Text Selection Handler of EditText

Actually I want to hide ActionPopupWindow (popup having SELECT ALL, CLIPBOARD options) when user click on + icon(refer to the attached image).
ActionPopupWindow appears when user click on the Text Selection Handler(bubble) (which appears when user tap on the text in the EditText).
I have tried to use setTextIsSelectable() method of EditText but it is not working consistently.
Any help or guidance will be well appreciated.
UPDATE: To hide the Popup already opened and showing on the screen, you need to clear focus of the current EditText or focus on other view when you clicked the plus button. See the
example below:
iconPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yourEditText.clearFocus();
}
});
If you want the popup never shows up at the first place, there are many ways to do it.
The simplest way is disabling long click and selection feature:
yourEditText.setLongClickable(false);
yourEditText.setTextIsSelectable(false);
Second one is overriding action callback actions on your edittext:
yourEditText.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;
}
});
You can use them separately or together according to your case.
Also you can check other options from here

How to remove default context menu in Edit Text? [duplicate]

This question already has answers here:
Disable EditText context menu
(7 answers)
Closed 5 years ago.
I am using EditText in my Android application. When i long press on the EditText, i want to remove/hide all the default context menu like Copy,Cut,Select All etc. Only paste menu should be shown. I have tried with the solution in the below link, but along with Paste, SelectAll option is also showing. How can i remove it.
EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event
EditText yourEditText = (EditText) findViewById(R.id.your_edit_text);
yourEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback()
{
#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)
{
// Do your stuff here when you destroy
}
});
}
In your xml file, add following line to your respective editText:
android:textIsSelectable="false"
For further operations you can use that library:
https://github.com/neopixl/PixlUI

Disable copy paste in a android

I want to disable copy paste in all of my android page and no one cant long touch on my application .
android:longClickable="false" it dosent work.
This worked for me:
In xml, disable the long-click in the EditText:
android:longClickable="false".
Also, you have to return false from these methods:
mEditEext.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;
}
});
I think it will work fine. User can not able to copy your page to another app.
//Inside onResume and onDestroy
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText("");
Here, It will restrict for background process "Secondary app". But you can copy and paste it only inside your app.

Item title not displayed in portrait with SHOW_AS_ACTION_WITH_TEXT

In my activity, I have an action mode with a single item which has a title and an icon.
I want both the title and the icon to be displayed, so I use SHOW_AS_ACTION_WITH_TEXT and SHOW_AS_ACTION_ALWAYS flags.
In landscape orientation, it's fine. I have title + icon.
But in portrait orientation only the icon is displayed (though there's a lot of free space). Does anyone know what I could do to fix it ?
Note that the title is correctly displayed if I remove the icon.
Here is my sample code:
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActionMode(new Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.add("Item 1").setIcon(R.drawable.ic_launcher).setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
#Override
public void onDestroyActionMode(ActionMode mode) { }
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
});
}
}
Does anyone know what I could do to fix it ?
I doubt that you can. "Always" and "with text" are requests, not commands. The framework does not always honor either of them.
Instead of adding this menu item via code try to use this attribute:
android:showAsAction="always|withText"
and add it to the item in the menu.xml

Categories

Resources