Wednesday, October 5, 2016

android - Disable copy and paste in EditText

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6712d"
    >
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:hint="Write some text here."
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private EditText mEditText;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mEditText = (EditText) findViewById(R.id.et);

        /*
            ActionMode.Callback
                public static interface ActionMode.Callback

                Callback interface for action modes. Supplied to startActionMode(Callback), a
                Callback configures and handles events raised by a user's interaction
                with an action mode.

                An action mode's lifecycle is as follows:
                    onCreateActionMode(ActionMode, Menu) once on initial creation
                    onPrepareActionMode(ActionMode, Menu) after creation and any time the ActionMode is invalidated
                    onActionItemClicked(ActionMode, MenuItem) any time a contextual action button is clicked
                    onDestroyActionMode(ActionMode) when the action mode is closed

        */

        /*
            onCreateActionMode
                boolean onCreateActionMode (ActionMode mode, Menu menu)

                Called when action mode is first created. The menu supplied
                will be used to generate action buttons for the action mode.

                Parameters
                    mode : ActionMode: ActionMode being created
                    menu : Menu: Menu used to populate action buttons

                Returns
                    boolean true if the action mode should be created, false
                    if entering this mode should be aborted.
        */

        // Disable copy and paste from/to edit text
        mEditText.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;
            }
        });
    }
}