How to Stop Edittext from Gaining Focus When an Activity Starts in Android

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



Leave a reply



Submit