Detect clicking on the back button - when the keyboard is open

How to detect Android back button when opening keyboard?

I need to hide the list if the keyboard is hidden.

I 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 lines. the keyboard is not hiding properly. It takes 5 to 10 seconds.

How to solve this?

+4
source share
1 answer

You can try to override onBackPressed (see the official Documentation ) as follows:

 @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 myListView is a ListView object that you set earlier (e.g. in your onCreate method).

Hope it solves your question

-7
source

Source: https://habr.com/ru/post/1501465/


All Articles