How to Stop EditText from Gaining Focus at Activity Startup in Android?

When working on an Android project. Edittext immediately gets the focus and the soft keyboard is forced open. Edittext is forced on the open screen on mobile application.


Method 1: Adding the request focus from the Edittext.

 <androidx.appcompat.widget.AppCompatEditText

android:id="@+id/gfgSampleField"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:inputType="number">

 <requestFocus />

<!-- Add the above tag -->

 </androidx.appcompat.widget.AppCompatEditText>

 

Method 2: Add  the request focus from the Edittext.

 <androidx.appcompat.widget.AppCompatEditText

android:id="@+id/gfgSampleField"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:inputType="number">

// <requestFocus />

<!-- Remove the above tag -->

 </androidx.appcompat.widget.AppCompatEditText>

Method 3: Removing the focus programmatically.

Maybe you don't need focus removal from XML, sometimes focus removal during application run time.

  getWindow().setSoftInputMode
 (WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

 

Method 4: Using XML flags.

You may simply remove the focus in the XML file and use the focusable attribute from the XML.below code used.

  <EditText

android:id="@+id/gfgSampleField"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:inputType="number">

android:focusable="false"/>



Method 5: Hardcoding the android manifest file.

This simply works programmatically at removing focus but some do not work also write code in the manifest file below code.

 windowSoftInputMode="stateAlwaysHidden"

Happy coding! 

365Bloggy May 17, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive