Keyboard hides AutoCompleteTextView dropdown

3.1k views Asked by At

I have an AppCompatAutoCompleteTextView at the bottom of a DialogFragment.

On tablet (API 19) in landscape mode the dropdown is covered by the keyboard when there is only one element in the suggestion list. When there are more elements, the dropdown goes upwards, and works fine.

On mobile (API 22), there aren't any problems even when there's only one element in the suggestion list, the dropdown is always shown upwards.

I've already added android:windowSoftInputMode="adjustPan|stateHidden" to the activity in the Manifest.

How can I make the dropdown always go upwards or not to be covered by the keyboard?

3

There are 3 answers

0
Bali On
 dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                try {
                    if (view != null) {
                        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
                        if (!imm.hideSoftInputFromWindow(autoCompleteTextView.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)) {
                            //call dialog dismiss code here
                        }
                    }
                } catch (Exception e) {
                    ExceptionUtils.logException(e);
                }
            }
            return false;
        }
    });

hideSoftInputFromWindow returns true when keyboard is closed else false. So in first back press it'll close keyboard and in second back press it'll go in if condition and dismiss dialog there

0
Dhara Patel On
  public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager =
            (InputMethodManager) activity.getSystemService(
                    Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(
            activity.getCurrentFocus().getWindowToken(), 0);
}

public void setupUI(View view) {

    // Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(getActivity());
                return false;
            }
        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}

Add this line in your oncreate setupUI(rootView.findViewById(R.id.parent));

0
Raman On
Work around the below the completionThreshold. Hope it works for you!
<AutoCompleteTextView 
  android:id="@+id/someID" 
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:completionThreshold="1" />

or

autocomplete.setThreshold(2);