androidkotlintextfieldhint

Add hint text at the below of the input field


At the moment, I have the simplest one-page application with one data entry field. I looked at a lot of examples, but I could not find the answer to my question: how do I add hint text at the bottom of the input field?

<?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/editTextUserId"
        android:layout_width="347dp"
        android:layout_height="60dp"
        android:layout_marginBottom="20dp"
        android:ems="10"
        android:hint="User"
        android:inputType="textPersonName"
        android:text="@string/user_id"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/imageButton"
        app:layout_constraintEnd_toEndOf="@+id/editTextDeviveId"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/editTextSessionId"
        app:layout_constraintTop_toBottomOf="@+id/editTextDeviveId"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Add a Textview and Constrain it below your Edittext

    <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/editTextUserId"
        android:layout_width="347dp"
        android:layout_height="60dp"
        android:layout_marginBottom="20dp"
        android:ems="10"
        android:hint="User"
        android:inputType="textPersonName"
        android:text="@string/user_id"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/imageButton"
        app:layout_constraintEnd_toEndOf="@+id/editTextDeviveId"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/editTextSessionId"
        app:layout_constraintTop_toBottomOf="@+id/editTextDeviveId"
        />
    
    <TextView
        android:id="@+id/hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="user"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="14sp"
        app:layout_constraintStart_toStartOf="@+id/editTextUserId"
        app:layout_constraintTop_toBottomOf="@+id/editTextUserId" />
       </androidx.constraintlayout.widget.ConstraintLayout>
    

    Then if you want to show/hide it base on user interaction you can do something like this in your activity

    editTextUserId.setOnFocusChangeListener((view, b) -> {
            if(b){hint.setVisibility(View.VISIBLE);}
            else {hint.setVisibility(View.INVISIBLE);}
    
        });