Friday, February 16, 2018

android kotlin - EditText example

MainActivity.kt

package com.cfsuman.kotlinexamples

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


class MainActivity : AppCompatActivity() {

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

        // Set some text to the EditText
        button_set.setOnClickListener{
            edit_text.setText("Hello EditText setText() method")
        }


        // Get the EditText text on button click event
        button_get.setOnClickListener{
            // Ensure that the EditText is not empty
            val inputtedText = edit_text.text
            if(inputtedText.isNotEmpty()){ // EditText validation
                // Display the EditText text in toast message
                Toast.makeText(applicationContext,inputtedText,Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(applicationContext,"EditText is empty",Toast.LENGTH_SHORT).show()
            }
        }


        // Empty the edit text
        button_clear.setOnClickListener{
            edit_text.setText("")
        }


        // Set a text change listener for edit text object
        edit_text.addTextChangedListener(object: TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                // Do something after text changed
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // Do something before text changed on EditText
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // Do something on text changed in EditText
                // Display the EditText change text on TextView real time
                text_view.text = edit_text.text
            }
        })
    }
}
activity_main.xml

<?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:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="#d7ded7"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e3e9de"
        android:padding="10dp"
        android:textSize="18sp"
        />
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Input your name here"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/button_set"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set Text"
            />
        <Button
            android:id="@+id/button_get"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Get Text"
            />
        <Button
            android:id="@+id/button_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear EditText"
            />
    </LinearLayout>
</LinearLayout>