Apply Capitalization to Character, Word and Sentence to TextField in Android Jetpack Compose
Last Updated :
22 Mar, 2022
In Android, a TextField is a UI element used to collect textual input from the user. It is simply an empty box, where the user has to click to bring it in focus and a soft-keyboard gets invoked that can be used to type text input. By default, there are no rules for typing text in the TextField like input must be a number or character, or must have a letter, word, or sentence capitalization. However, we can invoke methods to help users capitalize all characters, or starting character of every word or the first character of a first word in a sentence to maintain language writing aspects.
So in this article, we will show you how you could apply Capitalization to Character, Word, and Sentence in TextField 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.geeksforgeeks.jccapitalization
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.KeyboardOptions
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.KeyboardCapitalization
import androidx.compose.ui.tooling.preview.Preview
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
MainContent()
}
}
}
@Composable
fun MainContent() {
Scaffold(
topBar = { TopAppBar(title = { Text( "GFG | Capitalization" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = { MyContent() }
)
}
@Composable
fun MyContent(){
var mInput by remember { mutableStateOf( "" ) }
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
TextField(
value = mInput,
onValueChange = { mInput = it },
label = { Text( "Enter Input" ) },
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Characters)
)
}
}
@Preview (showBackground = true )
@Composable
fun DefaultPreview() {
MainContent()
}
|
Output:
You can see that the text entered is in upper case when we invoked capitalization to characters only.
When we invoke capitalization to words, then the first letter of every word is capitalized.
And Similarly, when we invoke capitalization to a sentence, the first letter of the first word of every sentence is capitalized.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...