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?

Timon Devos
  • 433
  • 1
  • 6
  • 18
  • 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. – Manju May 10 '12 at 14:19

4 Answers4

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) { }
   });
Dr.Luiji
  • 5,581
  • 1
  • 13
  • 11
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

Community
  • 1
  • 1
AndroidNoob
  • 2,653
  • 2
  • 37
  • 51
1

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) {

        }
    });
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);
            }
        }
    });
ucMedia
  • 2,500
  • 4
  • 21
  • 35