How to Set the InputType For a TextField in Android Jetpack Compose?
Last Updated :
20 Mar, 2022
In Android Jetpack Compose, a TextField is used to take input from the user in the form of text. TextField when in focus or when clicked invokes a keypad which is technically termed a Keyboard or a Soft-Keyboard. Soft-Keyboard in general consists of all characters that include numbers, alphabets, special characters, emoticons, and space. However, one or more of these types may not be needed as input and so, the specifications of the Soft-Keyboard may need to be changed. In the below image, you will find many types of inputs that a Soft-Keyboard can be restricted to like Password, Number, Ascii, Email, NumberPassword, Phone, Text, and Uri.
So in this article, we will show you how you could set Text Field Input Type to Number 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.jctextfieldinputtype
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.KeyboardType
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 | TextField Input Type" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = { MyContent() }
)
}
@Composable
fun MyContent(){
var mText by remember { mutableStateOf( "" ) }
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
TextField(value = mText,
onValueChange = {mText = it},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
}
}
@Preview (showBackground = true )
@Composable
fun DefaultPreview() {
MainContent()
}
|
Output:
In the below image, you can see that TextField takes only numbers as input.
Share your thoughts in the comments
Please Login to comment...