Edit Text ChangeListener Kotlin

Action whenever the text is changed in the EditText View.

Application to implement a listener
TextWatcher object, for EditText to trigger an action on text change.

Create a new Project in Kotlin

Message can display outside of our application normal UI

1 Open Android Studio.
2 Go to File => New => New Project. Write application name . Then, check Include Kotlin Support and click next button.
3Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4 Then, select Empty Activity => click next => click finish.
5 You will get a newly created project successfully if you have followed steps properly.

Use the below code to Implement a Edittext with Kotlin

MainActivity.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        editTextSample.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable) {}

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {
                tvSample.setText("Text is : "+s)
            }
        })
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity">

    <LinearLayout
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:padding="10sp"
            android:gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            android:layout_height="wrap_content" />

        <EditText
                android:id="@+id/editTextSample"
                android:textSize="20sp"
                android:layout_marginTop="50dp"
                android:hint="Enter Text ..."
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>



        <TextView
                android:id="@+id/tvSample"
                android:layout_marginTop="20dp"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold"
                android:textSize="16dp"
                android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Output

Leave a Reply