Open In App

How to Remove TextField Padding in Android using Jetpack Compose?

Improve
Improve
Like Article
Like
Save
Share
Report

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 soft keyboard. In general, TextField is highly attributed for a better feel and appearance. However, we can use a pre-developed TextField, named BasicTextField, which has no attributes like that of a regular TextField.

TextField Padding in Android

 

So in this article, we will show you how you could implement a BasicTextField that has no padding around it 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.jcbasictextfield
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
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.unit.dp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Implementing a Scaffold Layout
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Remove TextField Padding", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
                content = {
  
                    // Creating a Column View
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Declaring variables for two TextField Values
                        var mInput1 by remember{ mutableStateOf("")}
                        var mInput2 by remember{ mutableStateOf("")}
  
                        // Creating a Regular TextField
                        Text("Padded TextField")
                        TextField(
                            value = mInput1,
                            onValueChange = { mInput1 = it }
                        )
  
                        // Adding a Space of 100dp
                        Spacer(modifier = Modifier.height(100.dp))
  
                        // Creating a Non-Padded/Basic TextField
                        Text("Non-Padded TextField")
                        BasicTextField(
                            value = mInput2,
                            onValueChange = { mInput2 = it }
                        )
                    }
                }
            )
        }
    }
}


Output:

In the output, you can see two TextFields. We have entered “hello geek” in both the TextFields. The first one is the Regular TextField which has padding all around it. In the second one, you will see no padding at all.

Output

 



Last Updated : 30 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads