Android EditText Koltin

In Android EditText is user interface for entering and modifying text , when using you must specify the type of data that EditText accept  using  android:inputType .



Project Detail
Project Name AndroidEditText
Package com.tutorialsbuzz.androidedittext
Min Sdk Version 22
Target Sdk Version 28
Compile Sdk Version 28
Theme Theme.AppCompat.Light.DarkActionBar

1. gradle


file : build.gradle (Application Level Gradle)
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.tutorialsbuzz.androidedittext"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}


2. XML Layout


file : 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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        android:gravity="center"
        android:padding="10dp"
        android:id="@+id/rootLayout">

    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_text"
            android:id="@+id/editText"
            android:maxLines="1"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:padding="10dp"
    />

</LinearLayout>


  • android:inputType : value of this attribute aspecifies the type of data that edittext accepts 
              here are all the possible values of android:inputType
  1. none
  2. text
  3. textCapCharacters
  4. textCapWords
  5. textCapSentences
  6. textAutoCorrect
  7. textAutoComplete
  8. textMultiLine
  9. textImeMultiLine
  10. textNoSuggestions
  11. textUri
  12. textEmailAddress
  13. textEmailSubject
  14. textShortMessage
  15. textLongMessage
  16. textPersonName
  17. textPostalAddress
  18. textPassword
  19. textVisiblePassword
  20. textWebEditText
  21. textFilter
  22. textPhonetic
  23. textWebEmailAddress
  24. textWebPassword
  25. number
  26. numberSigned
  27. numberDecimal
  28. numberPassword
  29. phone
  30. datetime
  31. date
  32. time

  • android:maxLines : Set Number of lines user can input
Note : You just need to make sure you have the attribute "android:inputType" set. It doesn't work without inputType.
  • android:hint Show hint text 

EditText on Editor Change Listener


android:imeOptions this attribute takes keyboard action as value
for example if you want to recognize when action DONE button has clicked ,

  1.  android:imeOptions attribute equal to “actionDone” ,
  2.  Set Editor Change Listener on EditText , override onEditorAction method To Recognize Action DONE.  
 editText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Toast.makeText(this@MainActivity, "You Have Entered: " + editText.text.toString(), Toast.LENGTH_SHORT)
                    .show()
                false
            } else {
                false
            }
        }

EditText on TextChange Listener

  1. TextChangeListener monitor whenever there is change of text in EditText
  2. TextChangeListener interface has 3 method afterTextChanged , beforeTextChanged , onTextChanged 

editText.addTextChangedListener(object : TextWatcher {
 override fun afterTextChanged(s: Editable?) {
  Log.d(TAG, "afterTextChanged: " + s.toString());
 }

 override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
  Log.d(TAG, "beforeTextChanged: " + s.toString());
 }

 override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
  Log.d(TAG, "onTextChanged: " + s.toString());
 }
})


3. Activity


file : MainActivity.kt
package com.tutorialsbuzz.androidedittext

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val TAG: String = "MainActivity";

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

        //EditText on Editor Change Listener
        editText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Toast.makeText(this@MainActivity, "You Have Entered: " + editText.text.toString(), Toast.LENGTH_SHORT)
                    .show()
                false
            } else {
                false
            }
        }

        //EditText on TextChange Listener
        editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                Log.d(TAG, "afterTextChanged: " + s.toString());
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                Log.d(TAG, "beforeTextChanged: " + s.toString());
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                Log.d(TAG, "onTextChanged: " + s.toString());
            }
        })

    }
}


No comments:

Post a comment