Android Kotlin - How to Remove Underline from EditText While Typing
This code demonstrates a way to remove the underline from an EditText field while the user is typing in Kotlin for Android development. The approach utilizes the InputType
property of the EditText widget.
Code Breakdown:
MainActivity.kt:
- This file defines the main activity of the application.
- It fetches references to two EditText widgets defined in the layout (
editText
andeditText2
). - In the
onCreate
method, it sets theinputType
property ofeditText2
to achieve the underline removal.
inputType
Property:- The
inputType
property of EditText controls the type of text the field allows and how it is displayed on the screen. - In this example, a combination of two flags is used:
InputType.TYPE_CLASS_TEXT
: This specifies that the field accepts plain text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
: This flag, though named "visible password," actually hides the underline while typing. It's a counterintuitive naming convention.
- The
activity_main.xml:
- This file defines the layout of the main activity screen using ConstraintLayout.
- It includes two EditText elements (
editText
andeditText2
). - Interestingly,
editText
also uses the combination oftext
andtextVisiblePassword
flags in itsinputType
attribute, achieving the same underline removal effect.
Summary
This code snippet provides a simple solution to hide the underline from an EditText field during user input in an Android application. While the naming of TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
might be confusing, it effectively accomplishes the desired outcome. This approach offers programmatic control over the visual appearance of the EditText element.
MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.text.InputType
import android.widget.EditText
class MainActivity : Activity() {
private lateinit var editText:EditText
private lateinit var editText2:EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
editText = findViewById(R.id.editText)
editText2 = findViewById(R.id.editText2)
// hide/remove underline programmatically while typing
// input type TYPE_TEXT_VARIATION_VISIBLE_PASSWORD do the job
editText2.inputType = InputType.TYPE_CLASS_TEXT or
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
}
}
activity_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#DCDCDC"
android:padding="32dp">
<!-- textVisiblePassword will remove underline while typing -->
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:inputType="text|textVisiblePassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - EditText change cursor color programmatically
- android kotlin - EditText change underline color programmatically
- android kotlin - EditText focus change listener
- android kotlin - EditText hide keyboard on lost focus
- android kotlin - EditText enter key listener
- android kotlin - EditText listener
- android kotlin - EditText TextWatcher
- android kotlin - ImageView circle crop
- android kotlin - Circular ImageView with border
- android kotlin - Circular ImageView programmatically
- android kotlin - Get battery percentage programmatically
- android kotlin - Get battery level programmatically
- android kotlin - Get battery voltage programmatically
- android kotlin - Get screen density programmatically
- android kotlin - Convert pixels to dp programmatically