aashima
aashima

Reputation: 1203

Need to prevent my AutoCompleteTextView from gaining focus at the start of Activity

I have an 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 gain focus when my Activity starts.

I tried giving the following to the parent layout:

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

giving a dummy item 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 setting:

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

to AutoCompleteTextView .

On searching I found results similar to:

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

but what I want is prevent it from getting focus and not hiding the keypad from appearing.

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

EDIT: Also attempted:

clearFocus();
requestFocus();

Upvotes: 3

Views: 1096

Answers (1)

Rohit ZP
Rohit ZP

Reputation: 212

Try combining 2 of your methods as follows:

<!-- 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 as and when you require.

Upvotes: 3

Related Questions