Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

EditText in Android using Jetpack Compose

Jetpack Compose is Android's modern UI toolkit for building native UI. It simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.

To use an EditText equivalent in Jetpack Compose, you'd be using the TextField composable. Here's how you can do it:

  • Set up Jetpack Compose:

Ensure you have Jetpack Compose set up in your Android project. Your build.gradle should have the necessary dependencies and Kotlin compiler plugin for Compose.

  • Using TextField:

Here's a simple example of how to use TextField in Jetpack Compose:

import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.window.setContent
import androidx.activity.ComponentActivity
import androidx.compose.material.*
import androidx.compose.ui.graphics.Color

@Composable
fun EditTextDemo() {
    var text by remember { mutableStateOf("Hello Compose") }

    TextField(
        value = text,
        onValueChange = {
            text = it
        },
        label = { Text("Enter something") },
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = Color.White
        )
    )
}

@Preview
fun PreviewEditTextDemo() {
    EditTextDemo()
}

In this code:

  • We define a state-backed property text which will hold the text value of our TextField.
  • We use the TextField composable, which is equivalent to EditText in the traditional Android view system.
  • The value property represents the current text, and onValueChange is a callback that updates our state-backed property whenever the text changes.
  • We also use the label to provide a hint (equivalent to hint in EditText).
  • The colors parameter can be used to customize the appearance, in this case setting a white background.
  • Integrate with an Activity:

To use this composable in an Activity:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                EditTextDemo()
            }
        }
    }
}

Remember to wrap your composable UI in MaterialTheme to provide theming to the components and benefit from Material design styling.

With Jetpack Compose, building UI becomes more straightforward and idiomatic to Kotlin, reducing boilerplate and increasing readability.

  1. Creating EditText with Jetpack Compose:

    • Description: Jetpack Compose simplifies UI creation in Android. To create an EditText in Jetpack Compose, you can use the TextField composable.
    • Code:
      import androidx.compose.foundation.text.KeyboardActions
      import androidx.compose.foundation.text.KeyboardOptions
      import androidx.compose.material3.OutlinedTextField
      import androidx.compose.material3.TextFieldDefaults
      import androidx.compose.material3.icons.Icons
      import androidx.compose.material3.icons.filled.MailOutline
      import androidx.compose.material3.icons.filled.Person
      import androidx.compose.runtime.Composable
      import androidx.compose.ui.Modifier
      import androidx.compose.ui.platform.LocalContext
      import androidx.compose.ui.text.input.ImeAction
      import androidx.compose.ui.tooling.preview.Preview
      
      @Composable
      fun EditTextExample() {
          OutlinedTextField(
              value = "Hello, World!",
              onValueChange = { /* Handle value change */ },
              label = { /* Label for the EditText */ },
              keyboardOptions = KeyboardOptions.Default.copy(
                  imeAction = ImeAction.Done
              ),
              keyboardActions = KeyboardActions(onDone = {
                  /* Handle Done action */
              }),
              leadingIcon = { Icons.Default.Person },
              trailingIcon = { Icons.Default.MailOutline },
              modifier = Modifier.fillMaxWidth()
          )
      }
      
      @Preview
      @Composable
      fun PreviewEditTextExample() {
          EditTextExample()
      }
      
  2. Handling User Input in Jetpack Compose EditText:

    • Description: Jetpack Compose provides an onValueChange callback to handle user input changes in the EditText.
    • Code:
      @Composable
      fun EditTextWithInputHandling() {
          var text by remember { mutableStateOf("Initial Text") }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle user input changes
              },
              label = { /* Label for the EditText */ },
              modifier = Modifier.fillMaxWidth()
          )
      }
      
  3. Customizing EditText Appearance in Jetpack Compose:

    • Description: You can customize the appearance of EditText in Jetpack Compose using various modifiers.
    • Code:
      @Composable
      fun CustomizedEditText() {
          var text by remember { mutableStateOf("Initial Text") }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle user input changes
              },
              label = { /* Label for the EditText */ },
              modifier = Modifier
                  .fillMaxWidth()
                  .padding(16.dp)
                  .background(Color.Gray)
                  .border(1.dp, Color.Black)
          )
      }
      
  4. Listening to Text Changes in Jetpack Compose EditText:

    • Description: Use onValueChange to listen to text changes in Jetpack Compose EditText.
    • Code:
      @Composable
      fun EditTextWithTextChangeListener() {
          var text by remember { mutableStateOf("Initial Text") }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle text changes
              },
              label = { /* Label for the EditText */ },
              modifier = Modifier.fillMaxWidth()
          )
      }
      
  5. Setting Up Keyboard Input Type in Jetpack Compose:

    • Description: You can set up the keyboard input type using keyboardOptions in Jetpack Compose.
    • Code:
      @Composable
      fun EditTextWithKeyboardType() {
          var text by remember { mutableStateOf("Initial Text") }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle user input changes
              },
              label = { /* Label for the EditText */ },
              keyboardOptions = KeyboardOptions.Default.copy(
                  keyboardType = KeyboardType.Password
              ),
              modifier = Modifier.fillMaxWidth()
          )
      }
      
  6. Using State for EditText Values in Jetpack Compose:

    • Description: Use remember and mutableStateOf to manage state for EditText values in Jetpack Compose.
    • Code:
      @Composable
      fun EditTextWithState() {
          var text by remember { mutableStateOf("Initial Text") }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle user input changes
              },
              label = { /* Label for the EditText */ },
              modifier = Modifier.fillMaxWidth()
          )
      }
      
  7. Styling and Theming EditText in Jetpack Compose:

    • Description: You can apply styling and theming to EditText in Jetpack Compose using the Modifier and custom styles.
    • Code:
      @Composable
      fun StyledEditText() {
          var text by remember { mutableStateOf("Initial Text") }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle user input changes
              },
              label = { /* Label for the EditText */ },
              modifier = Modifier
                  .fillMaxWidth()
                  .padding(16.dp)
                  .background(MaterialTheme.colorScheme.primary)
                  .border(1.dp, MaterialTheme.colorScheme.secondary)
          )
      }
      
  8. Working with Focus and Keyboard in Jetpack Compose EditText:

    • Description: You can control focus and keyboard interactions in Jetpack Compose using Modifier and focusRequester.
    • Code:
      @Composable
      fun EditTextWithFocusAndKeyboard() {
          var text by remember { mutableStateOf("Initial Text") }
          val focusRequester = remember { FocusRequester() }
      
          OutlinedTextField(
              value = text,
              onValueChange = {
                  text = it
                  // Handle user input changes
              },
              label = { /* Label for the EditText */ },
              modifier = Modifier
                  .fillMaxWidth()
                  .padding(16.dp)
                  .focusRequester(focusRequester)
          )
      
          // Set focus programmatically
          Button(onClick = { focusRequester.requestFocus() }) {
              Text("Set Focus")
          }
      }