6

How to detect android back key when keyboard is open?

I want hide listview when keyboard is hide.

i've used below code

final View activityRootView = findViewById(R.id.globallayout);

        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() 
            {

                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

                if (heightDiff > 55) 
                { 
//keyboard is showing.
                }
                else  {

                    if(PopUpLayoutList.getVisibility()==View.VISIBLE){
                                            PopUpLayoutList.setVisibility(View.GONE);
                    }
                }
            }
        });

But , if the list contains more than 500 rows . the keyboard not hide properly. its take 5 to 10 secs.

How to solve this?

RVG
  • 3,538
  • 4
  • 37
  • 64
  • @shijuB hi. thanks . its working well. please post it as answer. i will accept it. – RVG Sep 10 '13 at 14:05

1 Answers1

-6

You can try overriding onBackPressed (see official Documentation) like this:

@Override
public void onBackPressed(){
    super.onBackPressed();    //This will call the normal operation pressing
                              // Back key
    myListView.setVisibility(View.GONE);    //This will hide yout listView Object
}

Assuming that myListView is a ListView Object you've setted before (in your onCreate method for example).

Hope it solves your question

slk
  • 46
  • 5