Android How to adjust layout in Full Screen Mode when softkeyboard is visible

前端 未结 25 2256
后悔当初
后悔当初 2020-11-22 03:56

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme=\"@andro

25条回答
  •  借酒劲吻你
    2020-11-22 04:35

    I used Joseph Johnson created AndroidBug5497Workaround class but getting black space between softkeyboard and the view. I referred this link Greg Ennis. After doing some changes to the above this is my final working code.

     public class SignUpActivity extends Activity {
    
     private RelativeLayout rlRootView; // this is my root layout
     private View rootView;
     private ViewGroup contentContainer;
     private ViewTreeObserver viewTreeObserver;
     private ViewTreeObserver.OnGlobalLayoutListener listener;
     private Rect contentAreaOfWindowBounds = new Rect();
     private FrameLayout.LayoutParams rootViewLayout;
     private int usableHeightPrevious = 0;
    
     private View mDecorView;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_sign_up);
      mDecorView = getWindow().getDecorView();
      contentContainer =
       (ViewGroup) this.findViewById(android.R.id.content);
    
      listener = new OnGlobalLayoutListener() {
       @Override
       public void onGlobalLayout() {
        possiblyResizeChildOfContent();
       }
      };
    
      rootView = contentContainer.getChildAt(0);
      rootViewLayout = (FrameLayout.LayoutParams)
      rootView.getLayoutParams();
    
      rlRootView = (RelativeLayout) findViewById(R.id.rlRootView);
    
    
      rlRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
       @Override
       public void onGlobalLayout() {
        int heightDiff = rlRootView.getRootView().getHeight() - rlRootView.getHeight();
        if (heightDiff > Util.dpToPx(SignUpActivity.this, 200)) {
         // if more than 200 dp, it's probably a keyboard...
         //  Logger.info("Soft Key Board ", "Key board is open");
    
        } else {
         Logger.info("Soft Key Board ", "Key board is CLOSED");
    
         hideSystemUI();
        }
       }
      });
     }
    
     // This snippet hides the system bars.
     protected void hideSystemUI() {
      // Set the IMMERSIVE flag.
      // Set the content to appear under the system bars so that the 
      content
      // doesn't resize when the system bars hide and show.
      mDecorView.setSystemUiVisibility(
       View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
     }
     @Override
     protected void onPause() {
      super.onPause();
      if (viewTreeObserver.isAlive()) {
       viewTreeObserver.removeOnGlobalLayoutListener(listener);
      }
     }
    
     @Override
     protected void onResume() {
      super.onResume();
      if (viewTreeObserver == null || !viewTreeObserver.isAlive()) {
       viewTreeObserver = rootView.getViewTreeObserver();
      }
      viewTreeObserver.addOnGlobalLayoutListener(listener);
     }
    
     @Override
     protected void onDestroy() {
      super.onDestroy();
      rootView = null;
      contentContainer = null;
      viewTreeObserver = null;
     }
     private void possiblyResizeChildOfContent() {
      contentContainer.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
    
      int usableHeightNow = contentAreaOfWindowBounds.height();
    
      if (usableHeightNow != usableHeightPrevious) {
       rootViewLayout.height = usableHeightNow;
       rootView.layout(contentAreaOfWindowBounds.left,
        contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
       rootView.requestLayout();
    
       usableHeightPrevious = usableHeightNow;
      } else {
    
       this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
      }
     }
    }
    

提交回复
热议问题