Open In App

Fetch Text From Editable TextField in Android Jetpack Compose

Last Updated : 13 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, an EditText is a sub-class of TextView, a UI element used to take text input from the user. EditText is generally used to collect information from the user for storing details or giving inputs for performing a function. However, in Jetpack Compose, we have TextField to collect and display textual data.

In this article, we will show you how you could implement an editable TextField and fetch the user input and display it in another TextField in Android using Jetpack Compose.

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.fetchedittext
  
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
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.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            
              // Displaying the created elements
            MainContent()
        }
    }
}
  
// Creating a composable 
// function to display the Top Bar
// MyContent() is set as content
@Composable
fun MainContent(){
    Scaffold(
        topBar = {TopAppBar(
            title = {Text(
                "GFG | TextField Fetch Data",
                color = Color.White)},
            backgroundColor = Color(0xff0f9d58)) },
        content = { MyContent()}
    )
}
  
// Creating a composable function MyContent() 
// to display editable TextField and a Button
@Composable
fun MyContent(){
    
    // Getting local content
    val mContent = LocalContext.current
    
    // Creating a variable to store the
    // editable TextField value
    var mText by remember { mutableStateOf("") }
  
    // Creating an editable TextField,
    // storing the value in mText
    Column(Modifier.fillMaxWidth()) {
        TextField(
            value = mText,
            onValueChange = { mText = it },
            label = { Text("Enter something...") },
            modifier = Modifier
                .fillMaxWidth()
                .absolutePadding(10.dp, 100.dp, 10.dp, 0.dp)
        )
    }
  
    // Creating a Button to display a Toast 
    // consisting mText value upon click
    Box(Modifier.fillMaxSize(), Alignment.Center){
        Button(onClick = { Toast.makeText(mContent, mText, Toast.LENGTH_LONG).show() },
            colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58))) {
            Text(text = "Click", color = Color.White)
        }
    }
}
  
// Displaying preview 
// in Android Studio IDE
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}


Output:

In the following video, you can see that a Toast message displaying the editable TextField value is displayed when the Button is clicked.



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

Similar Reads