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:android="http://schemas.android.com/apk/res/android"
    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:android="http://schemas.android.com/apk/res/android"
    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:

How to Make TextView and EditText Selectable in Android?

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.

Similar Reads

Implementing this feature for TextView

Method 1...

Implementing this feature for EditText

...