Needed to prevent my AutoCompleteTextView from getting focus at the start of the activity

I have AutoCompleteTextView in my XML file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout 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"
    tools:context="com.android.aashima.premiumpoint.PremiumPoint">

        <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchByName"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:background="#ddd"
        android:hint="Search By Name"
        android:textColorHint="#fff"
        android:gravity="center"
        android:layout_alignTop="@+id/searchImageButton"
        android:layout_alignBottom="@+id/searchImageButton"
        android:layout_toLeftOf="@+id/searchImageButton"
        android:textColor="#fff"
        android:completionThreshold="1"
        android:imeOptions="actionDone"
        android:dropDownWidth="fill_parent"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchImageButton"
        android:src="@drawable/search40"
        android:layout_alignParentRight="true"
        android:padding="2dp"
        android:background="@color/material_blue_grey_950"/>

    </RelativeLayout>

      

I don't want it to focus when my activity starts.

I tried to give the following to the parent layout:

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"

      

gives a dummy element just above my autocompleteTextView:

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>

      

and installation:

        android:nextFocusUp="@id/searchByName"
        android:nextFocusLeft="@id/searchByName"

      

in AutoCompleteTextView.

In a search, I found results similar to:

android:windowSoftInputMode="stateHidden"
android:windowSoftInputMode="stateUnchanged"

      

but I want it not to focus and hide the keyboard.

I have tried many alternatives with no luck. Am I doing something wrong here? Thanks in advance.

EDIT: Also tried:

clearFocus();
requestFocus();

      

+3


source to share


1 answer


Try to combine your 2 methods like this:

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"/>

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchByName"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:padding="10dp"
    android:background="#ddd"
    android:hint="Search By Name"
    android:textColorHint="#fff"
    android:gravity="center"
    android:layout_alignTop="@+id/searchImageButton"
    android:layout_alignBottom="@+id/searchImageButton"
    android:layout_toLeftOf="@+id/searchImageButton"
    android:textColor="#fff"
    android:completionThreshold="1"
    android:imeOptions="actionDone"
    android:dropDownWidth="fill_parent"
    android:nextFocusUp="@id/searchByName"
    android:nextFocusLeft="@id/searchByName"/>

      

Now bring the dummy View and your AutoCompleteTextView to your java code and use:



 dummy.requestFocus();
     search.clearFocus();

      

and vice versa, when and when you need it.

+2


source







All Articles