How to Change TextField’s Highlighted Text Color in Android using Jetpack Compose?
In Android Jetpack Compose, Text is a UI element used to display characters, words, or sentences as strings on the screen. By default, this Text cannot be selected for copying. We have to create a SelectionContainer and then implement a Text inside it to make it selectable. When we try to select text from the SelectionContainer, the text gets highlighted with a pale blue color along with blue color handles, one at left and the other at the right of the selected span. Observe this in the below image.
In this article, we will show you how you could change the Selected Text Highlight Color 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.jctextselectioncolors import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* 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.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp 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 | TextSelectionColors Implementation" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) }, content = { MyContent() } ) } // Creating a composable function to // create two Images and a spacer between them // Calling this function as content // in the above function @Composable fun MyContent(){ // Create a value CustomTextSelectionColors // using TextSelectionColors constructor // Setting the handle color to blue and background to red val mCustomTextSelectionColors = TextSelectionColors( handleColor = Color.Blue, backgroundColor = Color.Red ) // Create a SelectionContainer // to create a selectable text. SelectionContainer { Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { // Normal text with no CustomTextSelectionColors Text(text = "Sample Text 1" , fontSize = 30 .sp) // Adding a spacer with 100dp height Spacer(modifier = Modifier.height( 100 .dp)) // Implementing CustomTextSelectionColors // and declaring Text inside it CompositionLocalProvider(LocalTextSelectionColors provides mCustomTextSelectionColors){ Text(text = "Sample Text 2" , fontSize = 30 .sp) } } } } @Preview (showBackground = true ) @Composable fun DefaultPreview() { MainContent() } |
Output:
In the below image, you will see two screenshots. In the first one, we selected the first text where SelectionTextColors was not applied. The background is pale blue color and the handles are in blue color which is by default. In the second screenshot, we selected the second text where SelectionTextColors was applied.