0

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"/>
  
James Z
  • 12,209
  • 10
  • 24
  • 44
Hassam Ullah
  • 79
  • 1
  • 10
  • https://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus – Yasiru Nayanajith Sep 12 '21 at 13:59
  • Does this answer your question? [How to stop EditText from gaining focus at Activity startup in Android](https://stackoverflow.com/questions/1555109/how-to-stop-edittext-from-gaining-focus-at-activity-startup-in-android) – Ivan Barayev Sep 12 '21 at 21:02

2 Answers2

0

In android, focus automatically sets to the first root element that has focusable property. The workaround is the enable focusable property to your container element and the page will focus it instead of the first input element, triggering the keyboard. You have to set focusable=true (type it as it is not available in option) and focusableInTouchMode=true to the immediate parent container.

focus setting

Abishek Stephen
  • 386
  • 1
  • 3
  • 15
0

Try adding this to the root layout.

    android:focusableInTouchMode="true"

or adding this code on your manifest inside the activity tag

android:windowSoftInputMode="stateUnchanged"
Ayush Sth
  • 317
  • 6
  • 11