Search code examples
javaandroidandroid-textinputedittext

How to make a TextInputEditText unfocused by default in android?


I have created a Registration form and I wish for no text field to gain focus automatically when the activity starts. I have tried to add to all of my text fields but it is not working.

android:focusedByDefault="false"

XML Output

        <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/bio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="3dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Bio"
            android:inputType="textLongMessage"
            android:minLines="3"
            android:transitionName="logo_username"
            android:focusedByDefault="false"/>
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:layout_marginVertical="3dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        app:counterEnabled="true"
        app:counterMaxLength="10">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="Password"
            android:transitionName="logo_username"
            android:focusedByDefault="false"/>
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:textColor="@color/white"
        android:background="@color/black"
        android:transitionName="logo_button"
        android:focusedByDefault="true"/>
  

Solution

  • Try adding this to the root layout.

        android:focusableInTouchMode="true"
    

    or adding this code on your manifest inside the activity tag

    android:windowSoftInputMode="stateUnchanged"