auto hides keyboard after scrolling ListView on android
Asked Answered
C

5

11

Im new on android please help me to auto hide after scrolling the listview here is my code but could not get right 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>

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);
        }
    });
Coffey answered 28/5, 2014 at 5:1 Comment(3)
stackoverflow.com/a/11656129Romilda
@Romilda what is problem with above code??Coffey
i think you are passing view, change it to activity, some times workRomilda
M
38

Try this..

why don't you use OnTouchListener for ListView like 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;
    }
});
Mariellemariellen answered 28/5, 2014 at 5:14 Comment(2)
this is hiding keyboard properly after touching listview but im not able to scroll the listCoffey
i got the solution on your above code 'return false' give the required solutionCoffey
T
2

It is better to use onScrollStateChanged instead of onScroll and by using scrollState == 0. So keyboard will hide when user is really scrolling.

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


        }
    });
Ticklish answered 30/3, 2017 at 8:52 Comment(1)
scroll state == 0 is idle? Probably want to use RecyclerView.SCROLL_STATE_DRAGGINGExasperation
S
0

Recyclerview,to hide the keyboard on scrolling - SCROLL_STATE_DRAGGING ( Mentioned by @doubleA )

    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if(newState == RecyclerView.SCROLL_STATE_DRAGGING){
            InputMethodManager imm = (InputMethodManager) recyclerView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(recyclerView.getWindowToken(), 0);
        }
    }

Spiers answered 2/2, 2020 at 9:42 Comment(0)
C
0
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            //Hide keyboard code
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            //Find the currently focused view, so we can grab the correct window token from it.
            View view = activity.getCurrentFocus();
            //If no view currently has focus, create a new one, just so we can grab a window token from it
            if (view == null) {
                view = new View(activity);
            }
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    });
Complicated answered 4/3, 2021 at 11:40 Comment(2)
This thread is related to ListView not RecyclerView.Feat
While this piece of code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users refer to and eventually apply this knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained.Simsar
H
-1

Try this one.

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 it helps. Cheers!

Hauck answered 28/5, 2014 at 5:17 Comment(1)
but this code only show keyboard for some milliseconds when im trying to search itemsCoffey

© 2022 - 2024 — McMap. All rights reserved.