Open In App

Add an Input Filter to the TextField in Android using Jetpack Compose

Last Updated : 30 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Jetpack Compose, a TextField is a UI element that lets users type in text as an input. This input can then be stored and used for various desired functions. As TextField has no character restrictions, users can type in any character. However, there can be a system where the application would accept only certain characters as input. In such a case, we need to filter out those characters using the regex function. So in this article, we will show you how you could add an input filter to the 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.jcinputfilters
  
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.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold 
            // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Input Filter", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Creating a variable to remember text value
                        var text by remember { mutableStateOf("") }
                          
                        // Calling the function FilteredTextField
                        FilteredTextField(
                            text = text,
                            onChanged = { text = it },
                            ignoredRegex = Regex("[qwerty123]")
                        )
                    }
                }
            )
        }
    }
}
  
// Creating a function to create Filtered Text Field
@Composable
fun FilteredTextField(
    text: String,
    onChanged: (String) -> Unit,
    ignoredRegex: Regex
) {
    TextField(value = text,
        onValueChange = {
            if (!it.contains(ignoredRegex)) onChanged(it)
        }
    )
}


Output:

You can see that we are able to filter out the programmed characters.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads