Open In App

How to Disable Copy Functionality in EditText in Android?

Last Updated : 14 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, EditText is used to let users type in text that can be numbers, characters, strings, or a paragraph. Data from EditText can be fetched and used to perform desired operations. As the primary input of an EditText is text, it can be manually typed or copied from another source and simply be pasted into it. Similarly, a text in EditText can be copied and used in other apps.

EditText in Android

So in this article, we will show you how you could disable the copy functionality in EditText in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add an EditText as shown below.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <EditText
        android:id="@+id/edit_text_1"
        android:layout_width="300sp"
        android:layout_height="50sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100sp"
        android:hint="Edit Text 2"/>
  
</RelativeLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package org.geeksforgeeks.disablecopypasteet
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing
        // the EditText from the layout
        val mEditText = findViewById<EditText>(R.id.edit_text_1)
  
        // Disable every mode to disable copy
        mEditText.customSelectionActionModeCallback = object : ActionMode.Callback {
            override fun onCreateActionMode(p0: ActionMode?, p1: Menu?): Boolean {
                return false
            }
  
            override fun onPrepareActionMode(p0: ActionMode?, p1: Menu?): Boolean {
                return false
            }
  
            override fun onActionItemClicked(p0: ActionMode?, p1: MenuItem?): Boolean {
                return false
            }
  
            override fun onDestroyActionMode(p0: ActionMode?) {
  
            }
        }
    }
}


Output:

You can see that we are unable to select the text in the EditText. this prevents it from being copied.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads