How to disable copy/paste from/to EditText
Asked Answered
D

31

153

In my application, there is a registration screen, where i do not want the user to be able to copy/paste text into the EditText field. I have set an onLongClickListener on each EditText so that the context menu showing copy/paste/inputmethod and other options does not show up. So the user won't be able to copy/ paste into the Edit fields.

 OnLongClickListener mOnLongClickListener = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // prevent context menu from being popped up, so that user
            // cannot copy/paste from/into any EditText fields.
            return true;
        }
    };

But the problem arises if the user has enabled a third-party keyboard other than the Android default, which may have a button to copy/paste or which may show the same context menu. So how do i disable copy/paste in that scenario ?

Please let me know if there are other ways to copy/paste as well. (and possibly how to disable them)

Any help would be appreciated.

Disarticulate answered 8/6, 2011 at 7:5 Comment(4)
If the "paste" operation comes from an IME, then you have no standard way of distinguishing it from normal keystrokes. One idea to try is to measure the time between each character's arrival and if the time is too short, then the characters are coming from a "paste" operation.Santiago
seems to be dirty soloution! worth a look though.Disarticulate
use android:longClickable="false"Tweezers
The conclusion for everyone seems to be that: you really can't do it nicely. However, for my personal purposes, I want to disable paste because I can't handle certain characters being present, and paste can allow them into my EditText. A solution then is to add a text changed listener, and in the afterTextChanged method, remove those characters if they're there. You can add multiple listeners, and thus create one that prevents the text from being too long, invalid characters, etc. This is not preferable. But if anyone was looking for a half decent workaround, I think this is it.Lafleur
R
122

If you are using API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.

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

Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).

Reata answered 8/9, 2012 at 14:2 Comment(8)
what about api level below 13?Burmeister
Dont understand either comments, this sample works api11+, pre-api11 there wasn't copy and paste IIRCJudaist
Not working for me.Paste button will appear in case of tapping on blue cursor indicator.Frivolous
Also not working for me. On Double tapping the menu of copy-paste is showing.Oogonium
this doesn't working anymore on android 6.0 ,check this answer #27870483Studbook
This solution is good(but not perfect), unfortunately, in my case it caused NPE in android part of the code (sometimes, during "monkey testing"), so I had to find another solution, see my answer belowFinny
It's work only when EditText is not empty, It's Doest not work when edittext have some texts.Burkhard
@Humble coder the above solution is not working moto g5 mobile and in OS oreo. the solution for all devices is use below method : edittext.setCustomInsertionActionModeCallback();Guardsman
C
144

Best method is to use:

etUsername.setLongClickable(false);
Collide answered 11/12, 2012 at 13:15 Comment(6)
Or, just in xml android:longClickable="false" :)Delois
Paste button will appear in case of tapping on blue cursor indicator.Frivolous
This will certainly prevent the view from being long clickable, but the editing controls can also be requested by double tapping on text, which means that this solution is not complete. Keep this in mind for your purposes.Cornell
Also, keyboard shortcuts could still work (Ctrl+C) w/external keyboards.Tedra
This does not work on Ice Cream Sandwich because the clipboard options can be opened by double tapping text, as well as long tapping.Guardianship
but I still want to select text, just without widgetCrampon
R
122

If you are using API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.

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

Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).

Reata answered 8/9, 2012 at 14:2 Comment(8)
what about api level below 13?Burmeister
Dont understand either comments, this sample works api11+, pre-api11 there wasn't copy and paste IIRCJudaist
Not working for me.Paste button will appear in case of tapping on blue cursor indicator.Frivolous
Also not working for me. On Double tapping the menu of copy-paste is showing.Oogonium
this doesn't working anymore on android 6.0 ,check this answer #27870483Studbook
This solution is good(but not perfect), unfortunately, in my case it caused NPE in android part of the code (sometimes, during "monkey testing"), so I had to find another solution, see my answer belowFinny
It's work only when EditText is not empty, It's Doest not work when edittext have some texts.Burkhard
@Humble coder the above solution is not working moto g5 mobile and in OS oreo. the solution for all devices is use below method : edittext.setCustomInsertionActionModeCallback();Guardsman
H
56

You can do this by disabling the long press of the EditText

To implement it, just add the following line in the xml -

android:longClickable="false"
Hanging answered 25/9, 2014 at 1:23 Comment(2)
problem was that my app user has a third party keyboard which has a copy and paste button.Disarticulate
another problem is you can select the text by double tap and it shows copy/paste againBuckler
M
41

I am able to disable copy-and-paste functionality with the following:

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

textField.setLongClickable(false);
textField.setTextIsSelectable(false);

Hope it works for you ;-)

Metsky answered 6/9, 2012 at 5:56 Comment(2)
This is the exact same solution I ended up with based on the other answers above. This should be marked as the correct solution since it handles the edge cases the others do notBanc
This option blocks the copy but you can still paste by clicking on the cursor.Dressingdown
H
16

Kotlin solution:

fun TextView.disableCopyPaste() {
    isLongClickable = false
    setTextIsSelectable(false)
    customSelectionActionModeCallback = object : ActionMode.Callback {
        override fun onCreateActionMode(mode: ActionMode?, menu: Menu): Boolean {
            return false
        }

        override fun onPrepareActionMode(mode: ActionMode?, menu: Menu): Boolean {
            return false
        }

        override fun onActionItemClicked(mode: ActionMode?, item: MenuItem): Boolean {
            return false
        }

        override fun onDestroyActionMode(mode: ActionMode?) {}
    }
}

Then you can simply call this method on your TextView:

override fun onCreate() {
    priceEditText.disableCopyPaste()
}
Hoffert answered 20/7, 2017 at 17:51 Comment(2)
Hi, I'm using this approach, but I'm getting Type mismatch error with this description Required:ActionMode.Callback! Found: on this part object: ActionMode.Callback. Any idea why it might not be working?Burp
Try object : android.view.ActionMode.Callback insteadSudduth
P
12

here is a best way to disable cut copy paste of editText work in all version

if (android.os.Build.VERSION.SDK_INT < 11) {
        editText.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                // TODO Auto-generated method stub
                menu.clear();
            }
        });
    } else {
        editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            public void onDestroyActionMode(ActionMode mode) {
                // TODO Auto-generated method stub

            }

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            public boolean onActionItemClicked(ActionMode mode,
                    MenuItem item) {
                // TODO Auto-generated method stub
                return false;
            }
        });
    }
Piers answered 31/3, 2014 at 8:9 Comment(1)
This worked for me, I just had to add @TargetApi(Build.VERSION_CODES.HONEYCOMB)Pyrogenous
C
11

In addition to the setCustomSelectionActionModeCallback, and disabled long-click solutions, it's necessary to prevent the PASTE/REPLACE menus from appearing when the text selection handle is clicked, as per the image below:

Text selection handle with paste menu

The solution lies in preventing PASTE/REPLACE menu from appearing in the show() method of the (non-documented) android.widget.Editor class. Before the menu appears, a check is done to if (!canPaste && !canSuggest) return;. The two methods that are used as the basis to set these variables are both in the EditText class:

A more complete answer is available here.

Coyotillo answered 6/3, 2015 at 7:8 Comment(2)
This is the CORRECT and COMPLETE solutionPustulant
In some devices instead of Paste Clipboard option is visible, acts as paste only. i checked links but i am able to prevent paste but not clipboard. any idea ?Ical
M
7

If you don't wan't to disable long click because you need to perform some functionality on long click than returning true is a better option to do so.

Your edittext long click will be like this.

edittext.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
            //  Do Something or Don't
            return true;
      }
});

As per documentation Returning "True" will indicate that long click have been handled so no need to perform default operations.

I tested this on API level 16, 22 and 25. Its working fine for me. Hope this will help.

Mash answered 24/7, 2017 at 8:7 Comment(1)
Good one. Alternatively, just set android:longClickable="false" in XMLSpear
C
4

Here is a hack to disable "paste" popup. You have to override EditText method:

@Override
public int getSelectionStart() {
    for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
        if (element.getMethodName().equals("canPaste")) {
            return -1;
        }
    }
    return super.getSelectionStart();
}

Similar can be done for the other actions.

Cystic answered 19/1, 2017 at 22:11 Comment(1)
can you tell for Clipboard disableIcal
D
4

I've tested this solution and this works

    mSubdomainEditText.setLongClickable(false);
    mSubdomainEditText.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;
      }
    });
Darreldarrell answered 3/8, 2019 at 23:0 Comment(1)
i used this code it prevent from clipboard,copy option.Thank youKylander
P
4

i added Extension Function in Kotlin language :

fun EditText.disableTextSelection() {
    this.setCustomSelectionActionModeCallback(object : android.view.ActionMode.Callback {
        override fun onActionItemClicked(mode: android.view.ActionMode?, item: MenuItem?): Boolean {
            return false
        }
        override fun onCreateActionMode(mode: android.view.ActionMode?, menu: Menu?): Boolean {
            return false
        }
        override fun onPrepareActionMode(mode: android.view.ActionMode?, menu: Menu?): Boolean {
            return false
        }
        override fun onDestroyActionMode(mode: android.view.ActionMode?) {
        }
    })
}

you can use it like this :

edit_text.disableTextSelection()

also added below line in your xml :

                android:longClickable="false"
                android:textIsSelectable="false"
Peyton answered 10/6, 2020 at 6:56 Comment(0)
L
3

https://github.com/neopixl/PixlUI provides an EditText with a method

myEditText.disableCopyAndPaste().

And it's works on the old API

Langouste answered 14/11, 2013 at 16:28 Comment(1)
This does exactly the same as the half-solution provided by https://mcmap.net/q/112161/-how-to-disable-copy-paste-from-to-edittext. See the code: github.com/neopixl/PixlUI/blob/master/Library/src/com/neopixl/… ... This approach still doesn't prevent the text selection handler from showing "PASTE" if there's text in the clipboard.Coyotillo
D
3

If you want to disable ActionMode for copy/pasting, you need to override 2 callbacks. This works for both TextView and EditText (or TextInputEditText)

import android.view.ActionMode

fun TextView.disableCopyPaste() {
  isLongClickable = false
  setTextIsSelectable(false)
  customSelectionActionModeCallback = object : ActionMode.Callback {
    override fun onCreateActionMode(mode: ActionMode?, menu: Menu) = false
    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu) = false
    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem) = false
    override fun onDestroyActionMode(mode: ActionMode?) {}
  }
  //disable action mode when edittext gain focus at first
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    customInsertionActionModeCallback = object : ActionMode.Callback {
      override fun onCreateActionMode(mode: ActionMode?, menu: Menu) = false
      override fun onPrepareActionMode(mode: ActionMode?, menu: Menu) = false
      override fun onActionItemClicked(mode: ActionMode?, item: MenuItem) = false
      override fun onDestroyActionMode(mode: ActionMode?) {}
    }
  }
}

This extension is based off above @Alexandr solution and worked fine for me.

Denmark answered 18/11, 2020 at 8:35 Comment(1)
this solution is more comprehensiveInsouciant
C
3

its very late but may it help someone .

add these lines in your edittext xml

android:longClickable="false"
android:textIsSelectable="false"
android:importantForAutofill="no"
Cockayne answered 7/3, 2022 at 7:5 Comment(1)
Thanks, its working for me to apply for TextInputEditText. Well answerBirchfield
A
2

@Zain Ali, your answer works on API 11. I just wanted to suggest a way to do in on API 10 as well. Since I had to maintain my project API on that version, I was constantly playing with the functions available in 2.3.3 and got a possibility to do it. I have share the snippet below. I tested the code and it was working for me. I did this snippet on an urgency. Feel free to improve the code if there are any changes that can be done..

// A custom TouchListener is being implemented which will clear out the focus 
// and gain the focus for the EditText, in few milliseconds so the selection 
// will be cleared and hence the copy paste option wil not pop up.
// the respective EditText should be set with this listener 
// tmpEditText.setOnTouchListener(new MyTouchListener(tmpEditText, tmpImm));

public class MyTouchListener implements View.OnTouchListener {

    long click = 0;
    EditText mEtView;
    InputMethodManager imm;

    public MyTouchListener(EditText etView, InputMethodManager im) {
        mEtView = etView;
        imm = im;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            long curr = System.currentTimeMillis();
            if (click !=0 && ( curr - click) < 30) {

                mEtView.setSelected(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mEtView.setSelected(true);
                        mEtView.requestFocusFromTouch();
                        imm.showSoftInput(mEtView, InputMethodManager.RESULT_SHOWN);
                    }
                },25);

            return true;
            }
            else {
                if (click == 0)
                    click = curr;
                else
                    click = 0;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mEtView.requestFocusFromTouch();
                        mEtView.requestFocusFromTouch();
                        imm.showSoftInput(mEtView, InputMethodManager.RESULT_SHOWN);
                    }
                },25);
            return true;
            }

        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            mEtView.setSelected(false);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mEtView.setSelected(true);
                    mEtView.requestFocusFromTouch();
                    mEtView.requestFocusFromTouch();
                    imm.showSoftInput(mEtView, InputMethodManager.RESULT_SHOWN);
                }
            },25);
            return true;
        }
        return false;
    }
Agrapha answered 12/10, 2012 at 14:29 Comment(0)
W
2

For smartphone with clipboard, is possible prevent like this.

editText.setFilters(new InputFilter[]{new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if (source.length() > 1) {
                return "";
            }  return null;
        }
    }});
Wilding answered 18/12, 2018 at 10:48 Comment(1)
This fails because auto-correct, at least on my device, sometimes wants to replace characters at the same time it adds the newly typed characters. When this happens, this code thinks it's a paste and the whole auto-correcting (underlined) word is erased. The fix is to see if the source length is the destination length plus one - in that case, it's ok to accept the characters. But this is a kludge and also has the effect of disabling the "tap a word" to do auto-complete since that is exactly like a paste operation.Creolacreole
E
1

Read the Clipboard, check against the input and the time the input is "typed". If the Clipboard has the same text and it is too fast, delete the pasted input.

Exemption answered 14/11, 2011 at 8:33 Comment(0)
I
1

the solution is very simple

public class MainActivity extends AppCompatActivity {

EditText et_0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et_0 = findViewById(R.id.et_0);

    et_0.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            //to keep the text selection capability available ( selection cursor)
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            //to prevent the menu from appearing
            menu.clear();
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
   }
}

--------> preview <---------

Inmate answered 7/7, 2018 at 10:21 Comment(0)
B
1

Try Following custome class for prevant copy and paste in Edittext

public class SegoeUiEditText extends AppCompatEditText {
private final Context context;


@Override
public boolean isSuggestionsEnabled() {
    return false;
}
public SegoeUiEditText(Context context) {
    super(context);
    this.context = context;
    init();
}

public SegoeUiEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    init();
}

public SegoeUiEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    init();
}


private void setFonts(Context context) {
    this.setTypeface(Typeface.createFromAsset(context.getAssets(), "Fonts/Helvetica-Normal.ttf"));
}

private void init() {

        setTextIsSelectable(false);
        this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
        this.setLongClickable(false);

}
@Override
public int getSelectionStart() {

    for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
        if (element.getMethodName().equals("canPaste")) {
            return -1;
        }
    }
    return super.getSelectionStart();
}
/**
 * Prevents the action bar (top horizontal bar with cut, copy, paste, etc.) from appearing
 * by intercepting the callback that would cause it to be created, and returning false.
 */
private class ActionModeCallbackInterceptor implements ActionMode.Callback, android.view.ActionMode.Callback {
    private final String TAG = SegoeUiEditText.class.getSimpleName();

    public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; }
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
    public void onDestroyActionMode(ActionMode mode) {}

    @Override
    public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
        menu.clear();
        return false;
    }

    @Override
    public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
        return false;
    }

    @Override
    public void onDestroyActionMode(android.view.ActionMode mode) {

    }
}

}

Burkhard answered 10/8, 2018 at 19:53 Comment(0)
L
1

The solutions above do not take into account pasting with hardware keyboards (Ctrl+v). The easiest solution is to set a TextWatcher on your EditText, and filter characters you want or don't want in the afterTextChanged method. This works for all situations, i.e. typed characters, pastes, auto suggestions and auto corrections.

Livesay answered 16/11, 2021 at 18:31 Comment(0)
H
1

Actually in my case i had to set the callback for both selection and insertion and only then i got the copy/paste pop-up to not appear anymore. Something like this :

 private void disableCopyPaste() {
    input.setLongClickable(false);
    input.setTextIsSelectable(false);
    final ActionMode.Callback disableCopyPasteCallback = 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) {
        }
    };
    input.setCustomSelectionActionModeCallback(disableCopyPasteCallback);
    input.setCustomInsertionActionModeCallback(disableCopyPasteCallback);
}
Highminded answered 3/2, 2022 at 9:7 Comment(0)
F
1

Rather than completely disabling all actions on the EditText, you may want to prevent only certain actions (like cut/copy, but not paste):

/**
 * Prevent copy/cut of the (presumably sensitive) contents of this TextView.
 */
fun TextView.disableCopyCut() {
    setCustomSelectionActionModeCallback(
        object : Callback {
            override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?) = false

            override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                menu?.apply {
                    removeItem(android.R.id.copy)
                    removeItem(android.R.id.cut)
                }
                return true
            }

            override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?) = false

            override fun onDestroyActionMode(mode: ActionMode?) {
                // no-op
            }
        }
    )
}

Actions that can be selectively removed:

removeItem(android.R.id.copy)
removeItem(android.R.id.cut)
removeItem(android.R.id.paste)
removeItem(android.R.id.shareText) // Share
removeItem(android.R.id.textAssist) // Open with Chrome
Fallfish answered 21/2, 2022 at 0:35 Comment(2)
Kotlin correction: if we are not returning anything, then why use 'apply' extension function instead of 'run'? 'run' is more correct here.Pleader
Both run and apply have return values. A return value is not needed/used in this case, so either extension is equally suitable.Fallfish
D
0

I found that when you create an input filter to avoid entry of unwanted characters, pasting such characters into the edit text is having no effect. So this sort of solves my problem as well.

Disarticulate answered 23/11, 2011 at 3:54 Comment(0)
F
0

Solution that worked for me was to create custom Edittext and override following method:

public class MyEditText extends EditText {

private int mPreviousCursorPosition;

@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    CharSequence text = getText();
    if (text != null) {
        if (selStart != selEnd) {
            setSelection(mPreviousCursorPosition, mPreviousCursorPosition);
            return;
        }
    }
    mPreviousCursorPosition = selStart;
    super.onSelectionChanged(selStart, selEnd);
}

}

Finny answered 13/1, 2017 at 13:50 Comment(0)
I
0

Try to use.

myEditext.setCursorVisible(false);

       myEditext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub

        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        public boolean onActionItemClicked(ActionMode mode,
                MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }
    });
Inhesion answered 25/1, 2018 at 13:23 Comment(0)
S
0

Who is looking for a solution in Kotlin use the below class as a custom widget and use it in the xml.

class SecureEditText : TextInputEditText {

/** This is a replacement method for the base TextView class' method of the same name. This method
 * is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
 * appears when triggered from the text insertion handle. Returning false forces this window
 * to never appear.
 * @return false
 */
override fun isSuggestionsEnabled(): Boolean {
    return false
}

override fun getSelectionStart(): Int {
    for (element in Thread.currentThread().stackTrace) {
        if (element.methodName == "canPaste") {
            return -1
        }
    }
    return super.getSelectionStart()
}

public override fun onSelectionChanged(start: Int, end: Int) {

    val text = text
    if (text != null) {
        if (start != text.length || end != text.length) {
            setSelection(text.length, text.length)
            return
        }
    }

    super.onSelectionChanged(start, end)
}

companion object {
    private val EDITTEXT_ATTRIBUTE_COPY_AND_PASTE = "isCopyPasteDisabled"
    private val PACKAGE_NAME = "http://schemas.android.com/apk/res-auto"
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    disableCopyAndPaste(context, attrs)
}

/**
 * Disable Copy and Paste functionality on EditText
 *
 * @param context Context object
 * @param attrs   AttributeSet Object
 */
private fun disableCopyAndPaste(context: Context, attrs: AttributeSet) {
    val isDisableCopyAndPaste = attrs.getAttributeBooleanValue(
        PACKAGE_NAME,
        EDITTEXT_ATTRIBUTE_COPY_AND_PASTE, true
    )
    if (isDisableCopyAndPaste && !isInEditMode()) {
        val inputMethodManager =
            context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        this.setLongClickable(false)
        this.setOnTouchListener(BlockContextMenuTouchListener(inputMethodManager))
    }
}

/**
 * Perform Focus Enabling Task to the widget with the help of handler object
 * with some delay
 * @param inputMethodManager is used to show the key board
 */
private fun performHandlerAction(inputMethodManager: InputMethodManager) {
    val postDelayedIntervalTime: Long = 25
    Handler().postDelayed(Runnable {
        [email protected](true)
        [email protected]()
        inputMethodManager.showSoftInput(
            this@SecureEditText,
            InputMethodManager.RESULT_SHOWN
        )
    }, postDelayedIntervalTime)
}

/**
 * Class to Block Context Menu on double Tap
 * A custom TouchListener is being implemented which will clear out the focus
 * and gain the focus for the EditText, in few milliseconds so the selection
 * will be cleared and hence the copy paste option wil not pop up.
 * the respective EditText should be set with this listener
 *
 * @param inputMethodManager is used to show the key board
 */
private inner class BlockContextMenuTouchListener internal constructor(private val inputMethodManager: InputMethodManager) :
    View.OnTouchListener {
    private var lastTapTime: Long = 0
    val TIME_INTERVAL_BETWEEN_DOUBLE_TAP = 30
    override fun onTouch(v: View, event: MotionEvent): Boolean {
        if (event.getAction() === MotionEvent.ACTION_DOWN) {
            val currentTapTime = System.currentTimeMillis()
            if (lastTapTime != 0L && currentTapTime - lastTapTime < TIME_INTERVAL_BETWEEN_DOUBLE_TAP) {
                [email protected](false)
                performHandlerAction(inputMethodManager)
                return true
            } else {
                if (lastTapTime == 0L) {
                    lastTapTime = currentTapTime
                } else {
                    lastTapTime = 0
                }
                performHandlerAction(inputMethodManager)
                return true
            }
        } else if (event.getAction() === MotionEvent.ACTION_MOVE) {
            [email protected](false)
            performHandlerAction(inputMethodManager)
        }
        return false
    }
}

}

Sealskin answered 14/1, 2020 at 12:49 Comment(0)
D
0

A widely compatible solution (from Android 1.5 onwards) is

@Override
public boolean onTextContextMenuItem(int id) {
    switch (id){
        case android.R.id.cut:
            onTextCut();
            return false;
        case android.R.id.paste:
            onTextPaste();
            return false;
        case android.R.id.copy:
            onTextCopy();
            return false;
    }
    return true;
}
Degradable answered 23/3, 2021 at 14:51 Comment(0)
P
0

After spending a lot of time, removing the paste option in ContextMenu of EditText I have followed the below code in Java.

NoMenuEditText.Java

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import androidx.appcompat.widget.AppCompatEditText;

/**
 * custom edit text
 */


public class NoMenuEditText extends AppCompatEditText {

    private static final String EDITTEXT_ATTRIBUTE_COPY_AND_PASTE = "isCopyPasteDisabled";
    private static final String PACKAGE_NAME = "http://schemas.android.com/apk/res-auto";

    public NoMenuEditText(Context context) {
        super(context);
    }

    public NoMenuEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        EnableDisableCopyAndPaste(context, attrs);
    }

    /**
     * Enable/Disable Copy and Paste functionality on EditText
     *
     * @param context Context object
     * @param attrs   AttributeSet Object
     */
    private void EnableDisableCopyAndPaste(Context context, AttributeSet attrs) {
        boolean isDisableCopyAndPaste = attrs.getAttributeBooleanValue(PACKAGE_NAME,
                EDITTEXT_ATTRIBUTE_COPY_AND_PASTE, false);
        if (isDisableCopyAndPaste && !isInEditMode()) {
            InputMethodManager inputMethodManager = (InputMethodManager)
                    context.getSystemService(Context.INPUT_METHOD_SERVICE);
            this.setLongClickable(false);
            this.setOnTouchListener(new BlockContextMenuTouchListener
                    (inputMethodManager));
        }
    }

    /**
     * Perform Focus Enabling Task to the widget with the help of handler object
     * with some delay
     */
    private void performHandlerAction(final InputMethodManager inputMethodManager) {
        int postDelayedIntervalTime = 25;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                NoMenuEditText.this.setSelected(true);
                NoMenuEditText.this.requestFocusFromTouch();
                inputMethodManager.showSoftInput(NoMenuEditText.this,
                        InputMethodManager.RESULT_SHOWN);
            }
        }, postDelayedIntervalTime);
    }

    /**
     * Class to Block Context Menu on double Tap
     * A custom TouchListener is being implemented which will clear out the focus
     * and gain the focus for the EditText, in few milliseconds so the selection
     * will be cleared and hence the copy paste option wil not pop up.
     * the respective EditText should be set with this listener
     */
    private class BlockContextMenuTouchListener implements View.OnTouchListener {
        private static final int TIME_INTERVAL_BETWEEN_DOUBLE_TAP = 30;
        private InputMethodManager inputMethodManager;
        private long lastTapTime = 0;

        BlockContextMenuTouchListener(InputMethodManager inputMethodManager) {
            this.inputMethodManager = inputMethodManager;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                long currentTapTime = System.currentTimeMillis();
                if (lastTapTime != 0 && (currentTapTime - lastTapTime)
                        < TIME_INTERVAL_BETWEEN_DOUBLE_TAP) {
                    NoMenuEditText.this.setSelected(false);
                    performHandlerAction(inputMethodManager);
                    return true;
                } else {
                    if (lastTapTime == 0) {
                        lastTapTime = currentTapTime;
                    } else {
                        lastTapTime = 0;
                    }
                    performHandlerAction(inputMethodManager);
                    return true;
                }
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                NoMenuEditText.this.setSelected(false);
                performHandlerAction(inputMethodManager);
            }
            return false;
        }
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        CharSequence text = getText();
        if (text != null) {
            if (selStart != text.length() || selEnd != text.length()) {
                setSelection(text.length(), text.length());
                return;
            }
        }
        super.onSelectionChanged(selStart, selEnd);
    }


    @Override
    public boolean isSuggestionsEnabled() {
        return false;
    }

    @Override
    public int getSelectionStart() {
        for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
            if (element.getMethodName().equals("canPaste")) {
                return -1;
            }
        }
        return super.getSelectionStart();
    }



}

MainActivity

import androidx.appcompat.app.AppCompatActivity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    NoMenuEditText edt_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edt_username = (NoMenuEditText) findViewById(R.id.edt_username);

   
        edt_username.setLongClickable(false);
        edt_username.setTextIsSelectable(false);

        edt_username.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) {

            }
        });
        
    }

}

drawable- zeropx.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="0dp"
        android:height="0dp"/>

</shape>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NoMenuEditText">
        <attr name="isCopyPasteDisabled" format="boolean" />
    </declare-styleable>
</resources>

At Last, I finally Removed the paste option from the Context Menu of EditText

Thank you StackOverflow posts and http://androidinformative.com/disabling-context-menu/

Picturize answered 24/8, 2021 at 18:37 Comment(0)
R
0
 editText.apply {
        setOnTouchListener { v, event ->
               if (event.action == KeyEvent.ACTION_DOWN) {
                      requestFocus()
                      setSelection(text.toString().length)
                      showKeyboard()
                      return@setOnTouchListener true
               }
        }
 }

fun View.showKeyboard() {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(this, 0)
 }
Responsum answered 3/10, 2021 at 21:0 Comment(0)
S
-1

Similar to GnrlKnowledge, you can clear the Clipboard

http://developer.android.com/reference/android/text/ClipboardManager.html

If you want, preserve the text in the Clipboard, and on onDestroy, you can set it again.

Scowl answered 23/11, 2011 at 2:16 Comment(0)
P
-1

You may try android:focusableInTouchMode="false".

Pontormo answered 20/4, 2016 at 15:18 Comment(1)
It will never WorkMauri

© 2022 - 2024 — McMap. All rights reserved.