Open In App

How to Set the InputType For a TextField in Android Jetpack Compose?

Last Updated : 20 Mar, 2022
Like Article

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.


package com.geeksforgeeks.jctextfieldinputtype
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
              // Calling the composable function 
              // to display element and its contents
// Creating a composable 
// function to display Top Bar
fun MainContent() {
        topBar = { TopAppBar(title = { Text("GFG | TextField Input Type", 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
fun MyContent(){
      // Declaring a string variable 
      // to store the TextField input
    var mText by remember { mutableStateOf("") }
    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
          // Creating a simple TextField and declaring the 
          // keyboardOptions to take input only as a Number
          TextField(value = mText,
            onValueChange = {mText = it},
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
// For displaying preview in 
// the Android Studio IDE emulator
@Preview(showBackground = true)
fun DefaultPreview() {


In the below image, you can see that TextField takes only numbers as input.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads