Skip to main content

Android EditText with Examples in Kotlin

In Android, EditText is a standard entry widget. A user interface element for entering and modifying text. When you define an edit text widget, you must specify the R.styleable.TextView_inputType attribute. For example, for plain text input set inputType to "text", In case if EditText field is for password, then we need to specify the inputType as “textPassword”.

<EditText
     android:id="@+id/plain_text_input"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:inputType="text"/>

See the Text Fields guide for examples of other R.styleable.TextView_inputType settings.


Text Fields in Android Studio are basically EditText and we drag and drop or we can create edittext.

 First, we will check the drag and drop option here


EditText instance by declaring it inside a layout XML file

<EditText
     android:id="@+id/plain_text_input"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:inputType="text"/>

Attributes of EditText:

Now let’s  we discuss few attributes that helps us to configure a EditText in your XML.

AttributeDescription
android:idIt is used to uniquely identify the control
android:gravityIt is used to specify how to align the text like left, right, center, top, etc.
android:textIt is used to set the text.
android:hintIt is used to display the hint text when text is empty
android:textColorIt is used to change the color of the text.
android:textColorHintIt is used to change the text color of hint text.
android:textSizeIt is used to specify the size of the text.
android:textStyleIt is used to change the style (bold, italic, bolditalic) of text.
android:backgroundIt is used to set the background color for edit text control
android:emsIt is used to make the textview be exactly this many ems wide.
android:widthIt makes the TextView be exactly this many pixels wide.
android:heightIt makes the TextView be exactly this many pixels tall.
android:maxWidthIt is used to make the TextView be at most this many pixels wide.
android:minWidthIt is used to make the TextView be at least this many pixels wide.
android:textAllCapsIt is used to present the text in all CAPS
android:typefaceIt is used to specify the Typeface (normal, sans, serif, monospace) for the text.
android:textColorHighlightIt is used to change the color of text selection highlight.
android:inputTypeIt is used to specify the type of text being placed in text fields.
android:fontFamilyIt is used to specify the fontFamily for the text.
android:editableIf we set false, EditText won't allow us to enter or modify the text


Now open an activity_main.xml file from \res\layout path and write the code like


<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="Email"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

    <EditText
        android:id="@+id/editTextTextPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextEmailAddress" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Show Text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now open app -> java -> package -> MainActivity.kt and add the below code. In this we just fetch the text from the edittext, further with the button click event a toast will show the text fetched before.
package com.examlpe.helloworld

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    private lateinit var button: Button
    private lateinit var editTextName : EditText
    private lateinit var editTextEmail: EditText
    private lateinit var editTextPassword : EditText
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button = findViewById(R.id.buttonSubmit)
        editTextName = findViewById(R.id.editTextTextPersonName)
        editTextEmail = findViewById(R.id.editTextTextEmailAddress)
        editTextPassword = findViewById(R.id.editTextTextPassword)

        button.setOnClickListener {

            if ( editTextName.text.toString().isEmpty()
                || editTextEmail.text.toString().isEmpty()
                || editTextPassword.text.toString().isEmpty()){

                Toast.makeText(this,"Please Fill All the Details",Toast.LENGTH_SHORT).show()

            }else{
                Toast.makeText(this, "Name -  " + editTextName.text.toString()
                        + " \n" + "Password -  " +  editTextEmail.text.toString().isEmpty()
                        + " \n" + "E-Mail -  " + editTextPassword.text.toString().isEmpty(), Toast.LENGTH_SHORT).show();
            }

        }

    }
}
Output of Android EditText Example
Now start the AVD in Emulator and run the App. You will see screen asking you to fill the data in required fields like name,email, password. Enter data and click on button. You will see the data entered will be displayed as Toast on screen. 



This is how we can use EditText control in android applications to allow the user to enter or modify the text based on our requirements

Comments

Popular posts from this blog

Android Spinner using Kotlin

 In this example, we’ll be implementing Material Spinners in our Android Application using Kotlin. Android Spinner is used to create a drop-down list on the screen. What is Android Spinner? Spinners provide a quick way to select one value from a list. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the  Spinner  object. You should usually do so in your XML layout with a  <Spinner>  element. For example: <Spinner     android:id = "@+id/planets_spinner"     android:layout_width = "match_parent"     android:layout_height = "wrap_content" /> Spinner Callback Events  AdapterView.onItemSelectedListener interface is used to trigger the Spinner click event callbacks.  It consists of two methods:   1.onItemSelected   2.onNothingSelected XML Layout Code The code for the act

Android Hello World Kotlin App for Beginners

In this tutorial, we will learn how to create a Hello World Android app in Kotlin  on Android studio using Kotlin programming language. Start Hello World Android Application by starting Android Studio . Select the Activity type and click next. Setup Project Provide an application name ('Hello World' in my case) and check 'Include Kotlin support ' and proceed also  Select Minimum SDK for android application and click finish. Kotlin Android App Project Structure Look at XML design layout Android Hello World Kotlin Code Let’s look at the layout file i.e.  activity_main.xml  looks like: <?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:layout_width= "match_parent" android:layout