Search code examples
javaandroidkeyboardscrollview

Android App Java - Can't change the height of scrollview when keyboard is shown


I'm doing an Android App in Java but I have a problem with my activity which contain few editTexts in a scrollView. When the keyboard is show, I want to reduce the height of my scrollview to keep my editTexts above the keyboard.

My layout

I use this listener to know if the keyboard appear or disappear :

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    Rect r = new Rect();
    activityRootView.getWindowVisibleDisplayFrame(r);
    int screenHeight = activityRootView.getRootView().getHeight();
    int keypadHeight = screenHeight - r.bottom;
    if (keypadHeight > screenHeight * 0.15) {
      System.out.println("SHOW");     
      scrollview.getLayoutParams().height=heightScreen-1200;
      }
    else{
      System.out.println("HIDE");
      scrollview.getLayoutParams().height=heightScreen-300;
      }

But when I click on an editText and when the keyboard appear, the height of the scrollView doesn't change and I must click on an other editText to make the scrollView change.

Anyone has an idea ?

Thank you !


Solution

  • I add this line scrollview.setMinimumHeight(heightScreen - 1200); just before this line scrollview.getLayoutParams().height=heightScreen-1200; and it works.