Android Kotlin - How to implement Text Watcher for an EditText
This code demonstrates how to monitor text changes within an EditText widget using Kotlin in an Android application. It achieves this functionality by implementing the TextWatcher interface and attaching it to the desired EditText.
Explanation:
Setting Up the Activity:
- The
MainActivityclass extends theActivityclass, which is the base class for most Android activities. - It defines two member variables:
editTextandtextViewof typesEditTextandTextViewrespectively, to hold references to the corresponding UI elements from the layout.
- The
Initializing the Layout:
- In the
onCreatemethod, the activity inflates the layout fileactivity_main.xmlusingsetContentView. - It then retrieves references to the
EditTextandTextViewusingfindViewByIdand assigns them to the member variables.
- In the
Implementing Text Watcher:
- An anonymous inner class that implements the
TextWatcherinterface is created. - This interface has three callback methods:
beforeTextChanged: This method is called before the text is changed but after any selection is changed. (Not used in this example)onTextChanged: This method is called after the text has been changed and after any selection is changed. Here, it updates thetextViewcontent with the current text from theEditTextalong with a message.afterTextChanged: This method is called after the text has been changed, but before any selection is changed. (Not used in this example)
- An anonymous inner class that implements the
Attaching Text Watcher:
- The
addTextChangedListenermethod of theeditTextis called. This method takes aTextWatcherobject as a parameter. Here, the anonymous inner class implementingTextWatcheris passed as the argument.
- The
Layout File (activity_main.xml):
- This file defines the user interface for the activity using ConstraintLayout.
- It contains two elements:
- An
EditTextwhich allows the user to input text. - A
TextViewthat displays a message and the current text entered in theEditText.
- An
Summary
This code snippet showcases a basic implementation of monitoring text changes in an EditText. By attaching a TextWatcher to the EditText, the application can perform various actions based on user input, such as validating the entered text, updating other UI elements, or performing real-time calculations. The example demonstrates updating a TextView with the current text from the EditText.
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
class MainActivity : Activity() {
private lateinit var editText:EditText
private lateinit var textView:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)
// add text changed listener for edit text
editText.addTextChangedListener(object: TextWatcher{
// text watcher methods will be called when the text is changed
override fun beforeTextChanged(p0: CharSequence?,
p1: Int, p2: Int, p3: Int) {
// do something before text changed
}
override fun onTextChanged(p0: CharSequence?,
p1: Int, p2: Int, p3: Int) {
// do something on text changed
textView.text = "On text changed\n\n$p0"
}
override fun afterTextChanged(p0: Editable?) {
// do something after text changed
}
})
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#DCDCDC"
android:padding="32dp">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-condensed-medium"
android:gravity="center"
android:padding="8dp"
android:textColor="#1034A6"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - NumberPicker text size
- android kotlin - EditText space validation
- android kotlin - EditText email validation
- android kotlin - Show soft keyboard when EditText is focused
- android kotlin - EditText hide error
- android kotlin - EditText allow only numbers
- android kotlin - EditText allow only characters
- android kotlin - EditText limit number range
- android kotlin - EditText input filter decimal
- android kotlin - EditText remove underline while typing
- android kotlin - EditText change cursor color programmatically
- android kotlin - EditText hide keyboard after enter
- android kotlin - EditText hide keyboard click outside
- android kotlin - EditText enter key listener
- android kotlin - EditText listener