hide soft input when listview scroll
Asked Answered
I

2

5

The xml is as follow.

enter image description here

I want to implement the function like this: when I click the edittext, the soft input show. when I scroll(not scroll to OnScrollListener.SCROLL_STATE_IDLE state) the listview the soft input hide.

I use the android:windowSoftInputMode="adjustResize" .

Ingratiate answered 5/1, 2015 at 6:53 Comment(3)
try using :android:windowSoftInputMode="adjustPan"Assistant
Use adjustPan the titlebar will be out of the screen @krunal patelIngratiate
write this code on the scroll event of the listview: InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);Assistant
L
11

Detect your scroll using this link, it implements onScrollListener, which you will set to your ListView and in its onScrollStateChanged() you will put this code in your -

setOnScrollListener(new OnScrollListener(){
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      // TODO Auto-generated method stub
    }
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState !=0){
           InputMethodManager inputMethodManager = (InputMethodManager) 
           getSystemService(Activity.INPUT_METHOD_SERVICE);     
           inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(‌​), 0);
        }
    }
});
Langur answered 5/1, 2015 at 9:15 Comment(2)
I kept getting errors with this. Then I restarted Android Studio and reopened my project and all worked well. Android Studio can be buggy that way.Polack
requires API 23+Strangulate
W
1
InputMethodManager inputMethodManager = (InputMethodManager) 
           getSystemService(Activity.INPUT_METHOD_SERVICE);     
           inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(‌​), 0);

gives a bug in AS... Use this instead inside onScrollStateChange

InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(absListView.getApplicationWindowToken(), 0);
Wearing answered 1/7, 2017 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.