Show Error on the Tip of the Android EditText

EditText is used to take in Android user’s input. Showing error such as mis-matched password, wrong username is a must-have feature in this progress.

This is how to show error on the tip of an EditText.

<?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=".product.ProductListFragment"
    >
    
    <EditText
    android:id="@+id/etKeyword"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:ems="10"
    android:inputType="text"
    android:hint="@string/keyword"
    app:layout_constraintEnd_toStartOf="@+id/btnSearch"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    <ImageButton
        android:id="@+id/btnSearch"
        android:layout_width="@dimen/btn_search_width"
        android:layout_height="@dimen/btn_search_height"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:src="@android:drawable/ic_menu_search"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
btnSearch.setOnClickListener {
            val keyword = etKeyword.text.toString()
            if(keyword.length < 3){
                etKeyword.error  = "Keyword should contain at least 3 characters"
            } else {
                searchProductsByKeyword(keyword)
            }
        }

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close