I have a snippet containing 20+ lines to add personal information. The exact number of lines depends on how much the employer wants from users of the application.
If the number of lines that I need to show is larger than the available space, when showing the soft keyboard, I need to move other lines to the next screen, etc.
This was my game plan: Check how tall my lines + margins are (easy, hard-coded) Check the overall height of the screen Check the height of the soft keyboard
This will give me enough information to calculate ... you will get an image :)
It seems to be something like "getSoftKeyboardHeight", so I used this code in my snippet:
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); root = getActivity().getWindow().getDecorView().findViewById(R.id.fragment_container); root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { int heightDiff = root.getRootView().getHeight() - root.getHeight(); // IF height diff is more then 150, consider keyboard as visible. Log.d("slideactivity", "total " + root.getRootView().getHeight()); Log.d("slideactivity", "total " + root.getHeight()); Log.d("slideactivity", "heightdiff = " + heightDiff); } }); }
Log output:
08-13 12:37:55.242 D/slideactivity﹕ total 600 08-13 12:37:55.242 D/slideactivity﹕ total 543 08-13 12:37:55.242 D/slideactivity﹕ heightdiff = 57
While in fact half the screen is occupied by a soft keyboard. I started to wonder. Maybe the keyboard just appears when the values are printed, so I did not get the correct reading. I used the hacked Thread.Sleep in the async task before the measurement, but the same information appeared.
Does anyone know how to fix this problem? Maybe there is a library that does all the work? Or am I looking for solutions in the wrong places?
Any help is greatly appreciated since I already checked out a lot of stackoverflow answers!