Search code examples
androidtextinputlayoutmaterial-components-android

2 hints appearing when TextInputLayout is focused


I am having some trouble with the TextInputLayout.

When the TextInputLayout is focused, I am seeing the hint text float above the text field and also stay on the text field. Attaching images: enter image description here

And when I type stuff on the field, the text overlaps the hint on the text field.

enter image description here

This is the XML for my TextInputLayout

 <com.google.android.material.textfield.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="32dp" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="32dp" android:id="@+id/email"
        android:hint="hint">

    <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

I am using the android material components library like so:

implementation 'com.google.android.material:material:1.0.0'

How do I get the hint TextInputLayout to hide when it is focused on has text on it?

Here is the complete layout:

<?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:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.login.login.LoginFragment">
<ImageView
        android:layout_width="279dp"
        android:layout_height="289dp"
        android:src="@drawable/react"
        android:id="@+id/imageView" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
        app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
        android:layout_marginTop="24dp" app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="@string/vitrix_logo"/>
<com.google.android.material.button.MaterialButton
        android:text="@string/login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/loginButton"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="32dp" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="32dp" android:contentDescription="@string/click_to_login_to_the_app"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginBottom="16dp" app:layout_constraintBottom_toTopOf="@+id/createNewAccount"/>
<com.google.android.material.button.MaterialButton
        android:text="@string/create_account"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/createNewAccount"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="32dp" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="32dp" app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginBottom="32dp" app:layout_constraintBottom_toBottomOf="parent"/>
<com.google.android.material.textfield.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="32dp" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="32dp" android:id="@+id/email"
        android:hint="hint">

    <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

The base activity layout

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">


<fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:navGraph="@navigation/navigation_graph" app:defaultNavHost="true"
        android:id="@+id/fragment" android:layout_marginStart="8dp"
        app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp" android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"/>


Solution

  • Turns out I was just loading the fragment twice.

    I was loading in the Fragment once using navigation components and a second time from the main activity auto-generated boilerplate code you get when you use an Activity + Fragment template.

    To solve the issue, just remove the FragmentTrancaction.replace from your main activity and let the Nav Component load up the fragment for you.