How to Disable Text Selection in Android using Jetpack Compose?
In Android, a TextView is used to display text inside the activity on the screen. Similarly, in Jetpack Compose, a Text element is used to display text on the activity screen. By default, the text displayed in the Text element cannot be selected. To do so, the Text element has to be declared inside a SelectionContainer to make the text selectable. So, inside a SelectionContainer, if a Text element is declared, the text becomes selectable. However, we can disable text selection even inside a SelectionContainer upon a particular Text element if needed. So in this article, we will show you how you could Disable Text Selection in Selection Container in Android using Jetpack Compose. 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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. 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 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 com.w3wiki.jcautocorrectm import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.selection.DisableSelection import androidx.compose.foundation.text.selection.LocalTextSelectionColors import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.foundation.text.selection.TextSelectionColors import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.w3wiki.jcautocorrectm.ui.theme.JCAutoCorrectMTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Calling the composable function // to display element and its contents MainContent() } } } // Creating a composable // function to display Top Bar @Composable fun MainContent() { Scaffold( topBar = { TopAppBar(title = { Text( "GFG | Text Selection" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) }, content = { MyContent() } ) } // Creating a composable function to // create Selection container and Text inside it // Calling this function as content in the above function @Composable fun MyContent(){ // Creating a selection container SelectionContainer{ Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { // adding a space of height 100dp Text( "Selectable Text 1" , fontSize = 30 .sp) // adding a space of height 100dp Spacer(modifier = Modifier.height( 100 .dp)) // Creating a disableSelection DisableSelection { // Creating a text Text( "Non-Selectable Text" , fontSize = 30 .sp) } // adding a space of height 100dp Spacer(modifier = Modifier.height( 100 .dp)) // adding a space of height 100dp Text( "Selectable Text 2" , fontSize = 30 .sp) } } } // For displaying preview in // the Android Studio IDE emulator @Preview (showBackground = true ) @Composable fun DefaultPreview() { MainContent() } |
Output:
In the below recording, you can see that we are unable to select the second text.