Open In App

How to Make TextView and EditText Selectable in Android?

Last Updated : 21 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to implement a very important feature related to TextView. While using any social media app or like using Facebook you may have seen there is a particular type of TextView which you cannot copy like a caption that people write on their posts. You can select the message but there few texts which cannot select or copy. So here we are going to learn how to implement that feature.

Implementing this feature for TextView 

Method 1 

Step 1: 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. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".SelectText">
  
    <TextView
        android:id="@+id/select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Geeks For Geeks "
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp" />
     
</LinearLayout>


Step 2: Working with the MainActivity.java file

This is something that enables our text to be get selected and then we can copy that text

select.setTextIsSelectable(true);

This disables our text to be get selected or even by default it is set as false. Hence you cannot select a text in default mode

select.setTextIsSelectable(false);

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. 

Java




import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
  
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
  
import org.w3c.dom.Text;
  
public class SelectText extends AppCompatActivity {
    
    TextView select;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_text);
          
        select = findViewById(R.id.select);
        select.setTextIsSelectable(true);
    }
}


Method 2

Make Changes in the XML file. Add this line in your TextView.

android:textIsSelectable="true"

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".SelectText">
  
    <TextView
        android:id="@+id/select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:text="Geeks For Geeks "
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp" />
  
</LinearLayout>


Output:

Implementing this feature for EditText

Step 1: 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. We will create a simple EditText

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".SelectText">
  
    <EditText
        android:id="@+id/selecte"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:cursorVisible="false"
        android:textSize="30dp" />
  
</LinearLayout>


Step 2: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. In EditText By default, we can select text. Here firstly we will hide the cursor in the layout.xml file 

android:cursorVisible="false"

And added an eventlistener for long click and display the cursor only when a selection starts.

selecte.setCursorVisible(true);

Java




import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
  
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
  
import org.w3c.dom.Text;
  
public class SelectText extends AppCompatActivity {
    
    EditText selecte;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_text);
        
        selecte = findViewById(R.id.selecte);
  
        selecte.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
               selecte.setCursorVisible(true);
               Toast.makeText(SelectText.this,"Visible",Toast.LENGTH_LONG).show();
               return false;
            }
        });
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads