Wednesday, August 26, 2020

android kotlin - EditText listener

MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


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

        // list of colors
        val colors = listOf(
            "Brick red", "African violet","Baby pink",
            "Cameo pink","Barbie Pink","Canary yellow",
            "Brilliant rose","Candy pink","Bright green",
            "Almond","Eton blue","Cornell red"
        )

        // show all of the colors on text view
        textView.text = "List of colors"
        colors.forEach { textView.append("\n$it") }


        // 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 = "List filtered, on text changed\n"

                // filter the color list based on edit text input
                p0?.apply {
                    colors.filter {
                        it.startsWith(p0.trim(), true)
                    }.forEach {
                        // show the filtered list on text view
                        textView.append("\n$it")
                    }
                }
            }

            override fun afterTextChanged(p0: Editable?) {
                // do something after text changed
            }
        })
    }
}
activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEAE0"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        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="12dp"
        android:fontFamily="sans-serif-condensed-medium"
        android:gravity="center"
        android:padding="8dp"
        android:textColor="#4F42B5"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>