The Wayback Machine - https://web.archive.org/web/20240913234139/https://www.geeksforgeeks.org/how-to-make-textview-and-edittext-selectable-in-android/
Open In App

How to Make TextView and EditText Selectable in Android?

Last Updated : 21 Apr, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:



Similar Reads

How to Make Substring of a TextView Clickable in Android?
In this article, we are going to implement a very important feature related to TextView. Here we are making part of a string or a substring to act as a substring. This feature is important while writing a blog because it may be possible that for certain points we want to redirect the user to a link. So here we are going to learn how to implement th
2 min read
How to Make TextView Scrollable in Android?
In Android, a TextView is a primary UI element used to display text present in the form of numbers, strings, and paragraphs. However, for a large amount of information to be displayed size adjustment doesn't work. So in this article, we will show you how to make TextView scrollable on Android. Follow the below steps once the IDE is ready. TextView
8 min read
How to make Text Selectable in Android using Jetpack Compose?
By default, Composable Texts are not selectable which means users cannot copy text from your application, and to enable text selection you need to wrap the text elements into Selection Container. In this article, we will use Android’s Jetpack Compose to create those chips. A sample image is given below to give an idea of what we are going to build.
2 min read
How to Set Minimum and Maximum Input Value in EditText in Android?
In Android, EditText is a subclass of TextView which is configured to be editable. EditText is used for giving textual input such as characters, strings, numbers, etc. It has no limitation on the type of input unless explicitly attributed. Meaning we can attribute the EditText to accept only a string or a number. On top of this, we can create metho
3 min read
How to Search an Item in ListView using EditText and TextWatcher in Android?
Some applications provide a search bar for looking up any particular item from a list of items. Technically, a search bar is an EditText and an item list can be a ListView, RecyclerView, or a GridView that contains some items. Now, when a user types something in the EditText, the list must update in accordance with the text that the user types. Str
3 min read
Android EditText in Kotlin
EditText is used to get input from the user. EditText is commonly used in forms and login or registration screens. Following steps are used to create EditText in Kotlin: Add a EditText in activity_main.xml file.Add a Button in activity_main.xml file.Open MainActivity.kt file and set OnClickListener for the button to get the user input from EditText
3 min read
EditText widget in Android using Java with Examples
Widget refers to the elements of the UI (User Interface) that helps user interacts with the Android App. Edittext is one of many such widgets which can be used to retrieve text data from user. Edittext refers to the widget that displays an empty textfield in which a user can enter the required text and this text is further used inside our applicati
4 min read
How to Add Mask to an EditText in Android?
EditText is an android widget. It is a User Interface element used for entering and modifying data. It returns data in String format. Masking refers to the process of putting something in place of something else. Therefore by Masking an EditText, the blank space is replaced with some default text, known as Mask. This mask gets removed as soon as th
3 min read
Theming of Material Design EditText in Android with Example
Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire EditText field, helper text, etc. In this article, it'
4 min read
Key Properties of Material Design EditText in Android
In the previous article Material Design EditText in Android with Examples its been discussed how to implement the Material design EditText and how they differ from the normal EditText. In this article its been discussed on how to customize MDC edit text fields with their key properties. Have a look at the following image to get the idea about the k
4 min read
How to Customize AppCompat EditText in Android?
The EditText is one of the important UI element which takes the data as input from the user. The usual EditText in Android which looks decent enough has only the hint text and line which makes the user click on that line and insert the data. Refer to the article EditText widget in Android using Java with Examples, which explains basics about the no
4 min read
Working With the EditText in Android
EditText is one of the basic UI widgets, which is used to take the input from the user. The EditText is derived or is the extension of the TextView in Android. This article its been discussed in detail about the EditText in Android. The article also contains some of the redirections to other articles, also refer to them to get the detailed perspect
9 min read
How to Check Typing Status of EditText in Android?
In this article, we are going to implement an important and quite interesting feature. While Chatting with your friends over various social media platforms you may have found that if your friend is typing something then it shows Typing. Here are going to implement the same. While we start typing something, It will show typing, and when we erase eve
2 min read
How to Set Restriction For URL in Android EditText?
In this article, we are going to set restrictions in our EditText for URL. Whenever we are creating any form and we want to get the portfolio website of the user or even if we want to restrict the user to write the only URL then we can use this feature. This enables a user to user to write URL only. Step By Step Implementation Step 1: Create a New
2 min read
Invoking Search Button in Keyboard While Typing in EditText in Android
EditText can sometimes be a way of communicating with applications. Generally, EditTexts are used to fill in information or queries by the user and execute some tasks. Once the text is typed, the information or the query can be passed in the background to make things available or do some operations. But to pass it, there should be some method like
2 min read
How to Stop EditText from Gaining Focus at Activity Startup in Android?
When working on an Android Project, you might face a hazy error, the activity starts, and then the EditText immediately gets the focus and the soft keyboard is forced open. Well, that's not something that we expect, and let's address this, and get rid of it! Method #1: Removing the Request Focus from the EditText You may force override the focus of
2 min read
Restrict EditText Input to Some Special Characters in Android
In some of the other applications, most of us must have witnessed that while typing in some field, we get a warning or a flash message that certain characters are not allowed or are not accepted as valid input. For example, sometimes, while renaming a file or a folder in Windows, if we simply type in '?', we get a message that '?' is not accepted a
2 min read
How to Change Input Type of EditText Programmatically in Android?
EditText in Android UI or applications is an input field, that is used to give input text to the application. This input text can be of multiple types like text (Characters), number (Integers), etc. These fields can be used to take text input from the users to perform any desired operation. Let us say, a login activity in our application comprises
3 min read
How to Disable Copy Functionality in EditText in Android?
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
2 min read
How to Add Image on EditText in Android?
In Android, An EditText is an overlay over a TextView that makes it editable using text by invoking a soft keyboard in run time. It is generally implemented to collect text data from the user. EditText is most commonly used in forms that require users to fill in details and for passing a search query. In this article, we will show you how you could
3 min read
How to PopUp DatePicker While Clicking on EditText in Android?
In android applications date pickers are used to pick the date from the calendars and display them within our text view. Many times we have to display this date picker dialog box by clicking on edit text and then we have to display that date within our edit text. In this article, we will take a look at How to Pop Up a Date Picker Dialog in the andr
4 min read
Material Design EditText in Android with Example
EditText is one of the important UI elements. Edittext refers to the widget that displays an empty text field in which a user can enter the required text and this text is further used inside the application. In this article it has been discussed to implement the special type of text fields, those are called Material Design EditText. Have a look at
5 min read
Implement Form Validation (Error to EditText) in Android
Many of the already existing Android applications when it comes to the forms include user details. If the user enters the wrong information into the text fields or if the user leaves the text fields without filling them, there is a need to provide certain alert information to the TextFields which are unfilled and which contain the wrong information
6 min read
EditText in Android using Jetpack Compose
EditText is one of the most important widgets which is seen in most of the apps. This widget is generally used to get the data from users. Users can directly communicate with the app using this widget. This widget is used to get the data from the user in the form of numbers, text or any other text. In this article, we will take a look at the implem
8 min read
Android - Center Text in a TextView Horizontally and Vertically
Many android applications use text view for displaying text within android applications. There are different ways used to align the text view within our XML layout. To align TextView for different screen sizes we cannot use margin from all sides. For aligning the text view for different screen sizes we have to align them centrally to vertically and
2 min read
Zoom In and Out to TextView in Android
In this article, we are going to implement the zoom-in and zoom-out feature in TextView. As we zoom we will see the image after zooming in. Basically, we are going to learn how to increase or decrease the text size in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to impleme
2 min read
How to add fading TextView animation in Android
TextView is the basic building block of user interface components. It is used to set the text and display it to the user. It is a very basic component and used a lot. A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface than we can use Fading TextView. Approach: Add thi
1 min read
How to Display HTML in Textview along with Images in Android?
There are some situations when one needs to display rich formatted text in the app, like for a blog app or an app like Quora but Android's inbuilt function doesn't allow to display inline images by default, moreover they show the ugly blue line to the left of anything inside blockquote tag. Here is the simple solution to show HTML in TextView along
11 min read
How to Add a TextView with Rounded Corner in Android?
TextView is an essential object of an Android application. It comes in the list of some basic objects of android and used to print text on the screen. In order to create better apps, we should learn that how can we create a TextView which have a background with rounded corners. We can implement this skill in a practical manner for any android appli
2 min read
Expandable TextView in Android
ExpandableTextView is an Android library which allows us to easily create a TextView which can expand/collapse when user clicks on it .we can use this feature in many apps such as movie review app or storytelling app and in many other apps. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are go
4 min read
Article Tags :
Practice Tags :