How to stop EditText from gaining focus when an activity starts in Android?
Adding the tags android:focusableInTouchMode="true"
and android:focusable="true"
to the parent layout (e.g. LinearLayout
or ConstraintLayout
) like in the following example, will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
Disable auto focus on edit text
Add the android:focusable="true"
and android:focusableInTouchMode="true"
elements in the parent layout of EditText as follow;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout7" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true">
I think, it should help you.
See also;
Android Developers official guide on handling touch and input
Android : clear focus on edittext when activity starts
If Come back from other activity edittext get focused.
put these line onStart() or on onReusme()
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
Stop Decimal EditText from gaining focus on startup
Add this to your activity
tag in manifest
android:windowSoftInputMode="stateHidden|adjustResize"
Code looks like in manifest
<activity
android:name=".ActivityName"
android:windowSoftInputMode="stateHidden|adjustResize" />
This will help hide soft keyboard at startup
And add these tags to your root layout rather than parent layout
android:focusable="true"
android:focusableInTouchMode="true"
Stop EditText from bringing up the keyboard on Activity start?
Alternatively you can set the focus to the root layout element:
android:focusable="true"
android:focusableInTouchMode="true"
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Remove auto focus from EditText
Try this use android:windowSoftInputMode="stateAlwaysHidden"
in manifest file of your Activity
<activity
android:name=".Youractivity"
android:windowSoftInputMode="stateAlwaysHidden" />
EDIT
I have added two more line here
android:focusable="true"
android:focusableInTouchMode="true"
the above line is used for requesting focus rather than not focusing
why the on start activity - the focus is on my EditText and i can't remove it?
Add android:focusable="true"
& android:focusableInTouchMode="true"
to a parent element (LinearLayout
/RelativeLayout
/FrameLayout
/..) of your layout. Because the first focusable element in hierarchy gains the focus the parent element will take it and it should resolve your problem.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/companyInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/error_appearance">
<AutoCompleteTextView
android:id="@+id/company"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_company"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Related Topics
How to Start New Activity on Button Click
How to Use External Jars in an Android Project
Get Real Path from Uri, Android Kitkat New Storage Access Framework
Getting the Absolute File Path from Content Uri For Searched Images
How to Have Android Service Communicate With Activity
What APIs Are Used to Draw Over Other Apps (Like Facebook'S Chat Heads)
How to Save Users Score in Firebase and Retrieve It in Real-Time in Android Studio
Difference Between a View'S Padding and Margin
Call an Activity Method from a Fragment
Android Gradle Plugin 0.7.0: "Duplicate Files During Packaging of Apk"
Activity Has Leaked Window That Was Originally Added
How to Copy Files from 'Assets' Folder to Sdcard
Exception 'Open Failed: Eacces (Permission Denied)' on Android
Onactivityresult Method Is Deprecated, What Is the Alternative
Android Gallery on Android 4.4 (Kitkat) Returns Different Uri For Intent.Action_Get_Content
Android Min Sdk Version Vs. Target Sdk Version