How to disable copy/paste from/to EditText/TextView

1.8k views Asked by At

I was trying to make a Edittext which should NOT ALLOW user to copy content from or paste content to it.I read all ansers an do this but it is not work for me ! and when i get long click on text to select it, showing me copy/past .hear my code

video.TextViewSelectable class :

public class TextViewSelectable extends TextView {

OnSelectionChangedListener onSelectionChangedListener;

public TextViewSelectable(Context context, AttributeSet attrs,
                          int defStyle) {
    super(context, attrs, defStyle);
    disableCopyPast();

}

public TextViewSelectable(Context context, AttributeSet attrs) {
    super(context, attrs);
    disableCopyPast();


}

public TextViewSelectable(Context context) {
    super(context);
    disableCopyPast();

}


@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    if (onSelectionChangedListener == null){
        return;
    }
    String result=this.getText().subSequence(selStart, selEnd).toString();
    if(result.length()>1){
        onSelectionChangedListener.onSelectionChange(result);
    }
}

public void setOnSelectionChangedListener(OnSelectionChangedListener onSelectionChangedListener){
    this.onSelectionChangedListener = onSelectionChangedListener;
}

public void disableCopyPast() {
    this.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;
        }
    });
    this.setTextIsSelectable(true);
}

public interface OnSelectionChangedListener{
    public void onSelectionChange(String result);
}

}

xml code :

<video.TextViewSelectable
        android:id="@+id/offLine_subtitleText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="12"
        android:ellipsize="middle"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:clickable="true"
        android:textIsSelectable="true"
        android:textColorHighlight="#fffffe60"
        style="@android:style/Holo.ButtonBar"

        android:layout_gravity="center" 
        android:textSize="15sp"/>

usage:

    private  TextViewSelectable subtitleText;
 subtitleText =          (TextViewSelectable)findViewById(R.id.offLine_subtitleText);
  subtitleText.setOnSelectionChangedListener(this);
2

There are 2 answers

1
Gabe Sechan On

You can't, not entirely. Even if you block all those, if you use a keyboard such as swipe you can access copy and paste via the keyboard (swype key to c and swype key to v gestures), which does NOT use any of that functionality and can't be blocked.

0
EKN On

give below property in the edittext in your layout

android:longClickable="false"