EditText
Imagine a text box in a form where you type your name or password.
In Android, this input box is called EditText.
Example input fields:
Name: [________]
Email: [________]
Password: [________]
Those boxes are EditText views.
1. Definition
EditText is a UI component in Android that allows users to enter and edit text.
Simple meaning:
EditText = A text input field where users can type information.
2. Purpose
EditText is used to:
Take user input
Collect form data
Allow text editing
Accept search queries
Examples in apps:
| App Feature | EditText Usage |
|---|---|
| Login screen | Enter username |
| Registration | Enter email |
| Search bar | Enter search text |
| Chat app | Type messages |
3. Basic Syntax (XML)
Example layout code:
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
Explanation:
id→ unique identifierlayout_width→ width of input fieldlayout_height→ heighthint→ placeholder text
Example result:
Enter your name
[________________]
4. Using EditText in Kotlin Code
Example inside Activity:
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText = findViewById<EditText>(R.id.etName)
val userInput = editText.text.toString()
}
}
Explanation:
findViewById()connects Kotlin with XMLtext.toString()retrieves user input
5. Important Properties
1. hint
Displays placeholder text.
android:hint="Enter Email"
2. inputType
Defines the type of input.
Examples:
android:inputType="text"
android:inputType="number"
android:inputType="textPassword"
android:inputType="phone"
Example password field:
android:inputType="textPassword"
3. textColor
Changes text color.
android:textColor="#000000"
4. textSize
Changes text size.
android:textSize="18sp"
5. maxLength
Limits number of characters.
android:maxLength="10"
6. padding
Adds space inside EditText.
android:padding="10dp"
6. Important Methods (Kotlin)
getText()
Get user input.
val name = editText.text.toString()
setText()
Set text programmatically.
editText.setText("John")
or
editText.text = "John"
setHint()
Change hint text.
editText.hint = "Enter Username"
setEnabled()
Enable or disable input.
editText.isEnabled = false
setVisibility()
Show or hide the view.
editText.visibility = View.GONE
7. Real-Time Examples
Example 1: Login Form
XML
<EditText
android:id="@+id/etUsername"
android:hint="Username"/>
<EditText
android:id="@+id/etPassword"
android:hint="Password"
android:inputType="textPassword"/>
Result:
Username [________]
Password [________]
Example 2: Search Bar
<EditText
android:id="@+id/etSearch"
android:hint="Search products"
android:inputType="text"/>
Example in shopping apps.
Example 3: Get User Input
Kotlin code:
val nameInput = findViewById<EditText>(R.id.etName)
val name = nameInput.text.toString()
println("User Name: $name")
Example 4: Chat Message Input
[Type message here...] Send
EditText is used for typing messages in chat apps.
8. Advantages
✔ Accepts user input
✔ Supports multiple input types
✔ Easy to customize
✔ Essential for forms
9. Disadvantages
❌ Requires validation to ensure correct input
Example: email validation.
10. One-Line Summary
EditText is an Android UI component used to allow users to enter and edit text input.