AutoCompleteTextView: Remove soft keyboard on back press instead of suggestions
Asked Answered
H

2

23

When using AutoCompleteTextView, the dropdown suggestion list appears with the software keyboard still visible. This makes sense, as it is often a lot more efficient to type ensuing characters to narrow the list.

But if the user wants to navigate the suggestion list, it becomes extremely tedious with the software keyboard still up (this is even more of a problem when the device is in landscape orientation). Navigating the list is a lot easier without the keyboard hogging the screen space. Unfortunately, the default behaviour removes the list first when you press the back key (even though in the software versions of the back key it is showing the image that says 'pressing this will hide the keyboard').

Here's a barebones example that demonstrates what I'm talking about:

public class Main2 extends Activity {
    private static final String[] items = {
            "One",
            "Two",
            "Three",
            "Four",
            "Five"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AutoCompleteTextView actv = new AutoCompleteTextView(this);
        actv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        actv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
        actv.setThreshold(1);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView(actv);

        setContentView(ll);
    }
}

Besides the fact that this is unintuitive (the back key hint is suggesting that the back press will be sent to the keyboard), it makes navigating AutoCompleteTextView suggestions extremely tiresome.

What is the least intrusive way (e.g. catching the back in on onBackPressed() in every activity and routing it accordingly would definitely not be ideal) to make the first back press hide the keyboard, and the second remove the suggestion list?

Haemostasis answered 23/11, 2014 at 7:16 Comment(1)
Did you find a solution?Selfrenunciation
I
37

You can achieve that by override-ing onKeyPreIme in your custom AutoCompleteTextView.

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

    public CustomAutoCompleteTextView(Context context) {
        super(context);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {
            InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            
            if (inputManager.hideSoftInputFromWindow(findFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS)) {    
                return true;
            }
        }

        return super.onKeyPreIme(keyCode, event);
    }

}
Incarcerate answered 23/11, 2014 at 9:55 Comment(14)
Good solution but it doesn't seem to work. The key is correctly grabbed and recognised (i.e. hideSoftInputFromWindow is triggered) but the keyboard doesn't actually disappear. Could it be clashing with something in the EditText that keeps the soft keyboard up while the view has focus?Haemostasis
Can you try getApplicationWindowToken() insteadIncarcerate
Same result still unfortunatelyHaemostasis
I tested it on 4.4 and it is working fine, what is your setup? and testing deviceIncarcerate
Just tried it back in the blank project with only the code from the question and it works. There must be something clashing with this in my more complex instance. Nonetheless, thanks for the nice simple solution!Haemostasis
This has a drastic side effect - the next back press will now no longer remove the popup (isAcceptingText() is always true). As this question (#2150578) shows, adapting this to detect if keyboard is closed is not easy at all.Haemostasis
A workaround would be to use focus state as keyboard visibility indicator (review updated code) but you are right it can be unreliable you might have to track root view activity hight to tell if it is visible or not.Incarcerate
I've decided to add performClick() handle to mIsKeyboardVisible to trueEntropy
not work this solution seems that never called this methodGreenhead
Unfortunately, it doesn't work reliably - sometimes the keyboard gets closed, sometimes the suggestion dropdown gets closed instead. As long as no text has been entered, it's usually the keyboard that gets closed, but as soon as some characters have been entered, only the suggestion dropdown gets closed anymore...Labors
I've edited the code and it seems to work, at least on the few devices I've tried. With the current code, on a second back tap the popup is dismissed.Woven
You made my day. Thanks alot!Phenetidine
Awsomeeeeeeee!!Deb
MashaAllah, excellent, work in Android 10Singhalese
A
3

set DismissClickListener like this

 autoCompleteTextView.setOnDismissListener(new AutoCompleteTextView.OnDismissListener() {
            @Override
            public void onDismiss() {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);
            }
        });
Adley answered 23/10, 2015 at 6:48 Comment(1)
This works only after post API17. Any solution for API < 17 ? Thanks.Sall

© 2022 - 2024 — McMap. All rights reserved.