Automatically hides keyboard after scrolling ListView on android

Im new on android, please help me hide the car after scrolling through the list, here is my code, but could not get the correct solution.

xml file:

<ListView android:id="@+id/offline_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#dde1e3" android:clickable="true" android:focusable="true" > </ListView> 

the code:

  lvCustomList.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if(!hasFocus) hideKeyboard(v); } private void hideKeyboard(View view) { // TODO Auto-generated method stub InputMethodManager inputMethodManger = (InputMethodManager)getSystemService(Activity .INPUT_METHOD_SERVICE); inputMethodManger.hideSoftInputFromWindow(view.getWindowToken(), 0); } }); 
+7
android android-listview scroll keyboard
source share
3 answers

Try it.

why don't you use OnTouchListener for ListView as below

 lvCustomList.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0); return false; } }); 
+25
source share

It is better to use onScrollStateChanged instead of onScroll and using scrollState == 0 . This way, the keyboard will be hidden when the user really scrolls.

 listview.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == 0) { InputMethodManager inputMethodManger = (InputMethodManager) getActivity().getSystemService(Activity .INPUT_METHOD_SERVICE); inputMethodManger.hideSoftInputFromWindow(view.getWindowToken(), 0); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); 
+1
source share

Try it.

 listview.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { InputMethodManager inputMethodManger = (InputMethodManager)getSystemService(Activity .INPUT_METHOD_SERVICE); inputMethodManger.hideSoftInputFromWindow(view.getWindowToken(), 0); } }); 

Hope this helps. Hooray!

-one
source share

All Articles