How to Make Edittext Not Focused When Creating Activity

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"/>

How to make EditText not focused when creating Activity

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"

Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>

</RelativeLayout>

May this one helpful ;)

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: Force EditText to remove focus?

You can add this to onCreate and it will hide the keyboard every time the Activity starts.

You can also programmatically change the focus to another item.

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Prevent EditText from automatically focusing

Try this -this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

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"/>

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