Android EditText Tutorial With Example in Kotlin

Android EditText is in an android app basically use for taking input from users. It’s a very useful component(widget or controller) in app development. EditText is a subclass of TextView with text editing operations. The most common example of EditText is in the Login and signup process in the application.

Android EditText Tutorial With Example in Kotlin

In this tutorial, you will learn the following:

  1. An overview and basic of Android EditText
  2. Set EditText and get text example and many basic codes

An attributeandroid:inputType="text" is must specify, like for plain text input set inputType to "text". Why it’s a must? let’s see if your app has to accept a secret number, like a unique pin (password), you can set inputType to “numericPassword”. An inputType of “numericPassword” results in an edit text that accepts numbers only, shows a numeric keyboard.

EditText Syntax:

This is inside a layout resource file XML, you can also define it by pragmatically (java or kotlin)

Retrieving the Value From EditText:

Here is code in kotlin to retrieving the value from EditText in kotlin.

Set Hint in EditText

Setting a Hint in EditText is important, it gives an idea (information) about what to write on.

Set textColor in EditText

It depends on the user to set Text color in Theme or separate in each EditText widget. Always recommend defining value as color and string define in XML file value folders.

Let’s Build a Simple Example of Android EditText :

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Add EditText in resources layout “activity_main.xml

You can add by drag and drop or write direct code in the XML file.

Android EditText add in xml

add fowling code in  “activity_main.xml“, we are using Button to show perform get text form EditText and show in a toast.

Step 3. Open the “MainActivity.kt” and add the following code
Step 4. Now Run the application, in an emulator or On your Android device
Output screenshot Android EditText example :
Android EditText Tutorial With Example in Kotlin output

Download source code Android EditText in kotlin

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)



Coding in Kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.