Kotlin - Android Textwatcher Example

In this post we are going to learn how to use TextWatcher in Android.

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="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://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>

 

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

Android Textwatcher