Android Textwatcher - How do i use Textwatcher in Android Kotlin example

Last updated Oct 23, 2021

TextWatcher is an interface provided in Android framework which will be used to listen the text changes while we enter the text inside EditText widget. In this android example we are going to learn how to use TextWatcher in Android.

 

TextWatcher  consist of 3 abstract methods

  • afterTextChanged(Editable s)
  • beforeTextChanged(CharSequence s, int start, int count, int after)
  • onTextChanged(CharSequence s, int start, int before, int count)

By implementing these 3 methods we will handle the text inputs from the EditText . Normally we will use this textwatcher while implement search functionality, check the Text length inside EditText ...

 

In this we will create simple example which will display the text count inside EditText while we need to Text Limit to enter.

 

Let's Start

Step 1: Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project

 

Step 2: Update layout file with below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
                xmlns:tools="https://schemas.android.com/tools"
                android:layout_width="match_parent"
              android:orientation="vertical"
                android:layout_height="match_parent"
                tools:context=".TextWatcherActivity ">
    <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android Text Watcher"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginTop="24sp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true" />
    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="wrap_content">
        <EditText
                android:id="@+id/editInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:minHeight="200dp"
                android:gravity="left"
                android:padding="20dp"
                android:maxLength="200"
                android:hint="Write Your Message..." />
        <TextView
                android:id="@+id/textCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:text=""
                android:padding="10dp"
                android:textColor="#FF0000"
                android:layout_gravity="bottom|right"
                android:layout_centerHorizontal="true"

        />
    </FrameLayout>

    <TextView
            android:id="@+id/textDisplay"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="12dp"
          />
</LinearLayout>

 

 

We will count the Text entered inside edittext by using the beforeTextChanged method of the TextWatcher interface.

 

Step 3: Update Activity class with below code

class TextWatcherActivity : AppCompatActivity() {

    var count=200;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_text_watcher)
        editInput.addTextChangedListener(textWatcher)
        textCount.setText((200-editInput.text.toString().trim().length).toString())
    }

    var textWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            textDisplay.setText(s)
            textCount.setText((200-count).toString());
        }

        override fun afterTextChanged(s: Editable) {}
    }
}

 

Step 4: Let's run the application

How do i use Android TextWatcher in Android kotlin example

 

Conclusion: In this android example we covered how to work with Textwatcher interface in Android with kotlin example. Count the text enter inside EditText widget.

Related

Android search with Auto suggesion feature with TextWatcher

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2559 Views