Android EditText Kotlin

15-Sep-2024

Learn how to use the EditText widget for user input in Android studio project with Kotlin

Android EditText examples (plain text, password, password (numeric), email, phone, address, multiline text, time, date, numbers, etc.)


Here is the tutorial for Java


There Are Some Common Attributes for EditText :


id
This is unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
drawableLeft
Set Icon in Left
drawableRightSet Icon in Right
drawableTopSet Icon in Top
drawableBotomSet Icon in Botom
textStyle
bold, italic, normal -> Style Change
background
Set background Color




File open res/layout/activity_main.xml file −



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">


<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Name" />

<!--inputType= textEmailAddress,phone,textMultiLine,number, time, date-->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="12345678"
android:inputType="textPassword" /><!-- textEmailAddress,phone,textMultiLine,number, time, date-->


<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

<TextView
android:id="@+id/textViewShowName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Show Name" />

<TextView
android:id="@+id/textViewShowPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Show Password" />


</LinearLayout>



</RelativeLayout>


File open com.microappvalley.newtestproject/MainActivity.kt file −



package com.microappvalley.newtestproject

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

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

val editTextName:EditText=findViewById(R.id.editTextName)
val editTextPassword:EditText=findViewById(R.id.editTextPassword)
val buttonSubmit:Button=findViewById(R.id.buttonSubmit)
val textViewShowName:TextView=findViewById(R.id.textViewShowName)
val textViewShowPassword:TextView=findViewById(R.id.textViewShowPassword)

buttonSubmit.setOnClickListener {
textViewShowName.text=editTextName.text
textViewShowPassword.text = editTextPassword.text
}
}
}


Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:




Comments