Android Remove Soft Keyboard when touching the listview
Asked Answered
H

4

9

In my application I have a view with a listview and searchbar to search in the listview. When you tap the searchbar it gets the focus and the soft keyboard turns up. When I touch the listview the keyboard stays on top of the listview, therefore I can't see a lot of my listview.

My question: How do I know if the listview has been touched/scrolled/... and how do I remove the soft keyboard AND remove the focus from the edittext?

Handkerchief answered 25/4, 2012 at 14:23 Comment(1)
Hi i have question here if possible can you tell me by default your search bar is not showing the softkeyboard. Im my app as soon as activity starts softkey board is poping up.Ermina
K
3

Take a look at this question to find out how to close the keyboard, as for knowing if the listview has been scrolled, you can extend the listview class and override the onScrollChanged() method and to do whatever you want when they scroll is interacted with

Edit: there is actually an OnScrollListener to listen for scroll changes in a listview

Klan answered 25/4, 2012 at 14:37 Comment(0)
J
17

Based on @androidnoob answer, I post here (for the others having this specific issue) the full code needed.

list.setOnScrollListener(new OnScrollListener() {
     public void onScrollStateChanged(AbsListView view, int scrollState) {
             //hide KB
             InputMethodManager imm =  (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
             imm.hideSoftInputFromWindow(colleagueSearch.getWindowToken(), 0);
            }

            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { }
   });
Josie answered 10/1, 2013 at 9:15 Comment(0)
K
3

Take a look at this question to find out how to close the keyboard, as for knowing if the listview has been scrolled, you can extend the listview class and override the onScrollChanged() method and to do whatever you want when they scroll is interacted with

Edit: there is actually an OnScrollListener to listen for scroll changes in a listview

Klan answered 25/4, 2012 at 14:37 Comment(0)
T
0

yourListView.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState != 0){

                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

            }
        }
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }
    });
Targe answered 10/6, 2014 at 11:16 Comment(0)
M
0

@Dr.Luiji answer works, but I think it's better to hide softkey immediately after the user touches the listView.

    myListView.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(((Activity) mContext).getCurrentFocus().getWindowToken(), 0);
            }
        }
    });
Macule answered 17/11, 2018 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.