Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Remove Soft Keyboard when touching the listview

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?

like image 490
Timon Devos Avatar asked Apr 25 '12 14:04

Timon Devos


2 Answers

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) { }
   });
like image 68
Dr.Luiji Avatar answered Sep 21 '22 09:09

Dr.Luiji


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

like image 23
AndroidNoob Avatar answered Sep 22 '22 09:09

AndroidNoob