In Android development using Kotlin, the EditText is a versatile UI widget that allows users to input text. Here's a basic example of how to use EditText in a Kotlin activity:
In Android development using Kotlin, the EditText is a versatile UI widget that allows users to input text. Here's a basic example of how to use EditText in a Kotlin activity:
<!-- activity_main.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your text here" /> <Button android:id="@+id/showEt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="show" android:backgroundTint="@color/colorPrimary" android:textColor="@android:color/white" /> </RelativeLayout>
Step 2: Implement the EditText functionality in the Kotlin code.
// finding the button val showEt= findViewById<Button>(R.id.showEt) // finding the edit text val editText = findViewById<EditText>(R.id.editText)
Setting the on click listener to the button
showEt.setOnClickListener { }
Getting the text entered by user
val text = editText.text
Step 3: In the Kotlin file (e.g., MainActivity.kt), you'll need to find the EditText view by its ID and handle the user input:
import android.os.Bundle import android.widget.EditText import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val showEt= findViewById<Button>(R.id.showEt) val editText = findViewById<EditText>(R.id.editText) showEt.setOnClickListener { // Getting the user input val text = editText.text // Showing the user input Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } } }
In this example, we set a hint for the EditText to display a message ("Enter your text here") when it is empty.
Remember to replace activity_main.xml and MainActivity.kt with your actual file names. With this basic setup, your Android app will now have a functional EditText that captures user input!
In this example, we've set up a TextWatcher interface by overriding its three methods: beforeTextChanged, onTextChanged, and afterTextChanged. We're interested in the afterTextChanged method because it provides us with the final edited text after the user has made changes.
In the afterTextChanged method, we retrieve the user's input using the Editable parameter, check if it's not empty, and then display a toast message showing what the user has typed.
Implement the Kotlin code (MainActivity.kt):
import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.widget.EditText import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val editText = findViewById<EditText>(R.id.editText) // Set a TextWatcher to listen for text changes editText.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { // This method is called before the text is changed } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { // This method is called when the text is being changed } override fun afterTextChanged(s: Editable?) { // This method is called after the text has been changed val userInput = s?.toString() if (!userInput.isNullOrEmpty()) { // Show a toast message with the user's input Toast.makeText(applicationContext, "You typed: $userInput", Toast.LENGTH_SHORT).show() } } }) } }
When you run the app and start typing in the EditText, you will see toast messages showing the text you've entered!