Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView always keeps focus

I have 2 AutoCompleteTextViews in an activity (LinearLayout) and several additional controls (radiogroups, buttons, etc). Somehow the AutoCompleteTextViews are never losing focus.

As Example: The user clicks on an AutoCompleteTextView, the control gets the focus. So the cursor starts blinking, the autocomplete dropdown list and the keyboard is shown. This is fine. However if the user now clicks on of the radio buttons (or another control), the cursor in the AutoCompleteTextView is still blinking and the keyboard is still shown.

How to make the focus disappear automatically?

EDIT: xml code

                <AutoCompleteTextView
                android:id="@+id/ediFrom"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:singleLine="true"
                android:text="" />
like image 661
nr1 Avatar asked Feb 21 '14 16:02

nr1


3 Answers

Only solution that worked for me is to add this line

android:focusable="true" 
android:focusableInTouchMode="true"

To parent of AutoCompleteTextView (like LinearLayout etc..)

like image 57
Michal Heneš Avatar answered Oct 03 '22 23:10

Michal Heneš


have u tried with android:focusableInTouchMode="true" for each view code snippet

<AutoCompleteTextView
      android:id="@+id/ediFrom"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:singleLine="true"
      android:focusableInTouchMode="true"
      android:text="" />

http://android-developers.blogspot.in/2008/12/touch-mode.html

like image 22
Kaushik Avatar answered Oct 03 '22 23:10

Kaushik


In order to avoid setting everything else focusable (which is painful if you happen to use the same text view in many other layouts), we opt to override the logic to intercept touch screen events at activity level instead:

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View v = getCurrentFocus();
    if (v instanceof EditText) {
        int scrcoords[] = new int[2];
        v.getLocationOnScreen(scrcoords);
        // calculate the relative position of the clicking position against the position of the view
        float x = event.getRawX() - scrcoords[0];
        float y = event.getRawY() - scrcoords[1];

        // check whether action is up and the clicking position is outside of the view
        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < 0 || x > v.getRight() - v.getLeft()
                || y < 0 || y > v.getBottom() - v.getTop())) {
            if (v.getOnFocusChangeListener() != null) {
                v.getOnFocusChangeListener().onFocusChange(v, false);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}

If you put this logic in your base activity, any screen with an edit text now will fire onFocusChange when you tap anywhere outside it. By listening to onFocusChange you can clearFocus or requestFocus on another view. It's a hack more or less but at least you don't have to set focusable for any other items on many layouts.

See http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)

like image 21
hidro Avatar answered Oct 03 '22 22:10

hidro