skip to Main Content

I need to set the height of this RelativeLayout to match the height of the Soft Keyboard. To make sure the EditText appears just above the keyboard.

Is it possible to set some kind of Listener to detect every time the keyboard height changes?

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I figured out a solution that seems to work fine. With only 1 problem. For some reason the Status Bar turns white with this piece of code.. Any idea why?

    ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {
            
        int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
            
        //Code logic
            
        return insets;
    });
    

    How I set the Status Bar color:

    <style name="AppTheme.GuessWord">
        <item name="colorPrimaryDark">@color/primaryColor</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

  2. The proper way to do this would be to set soft input to match your needs.
    try following for a fragment put this line in your fragment’s onCreate method:

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    

    You can set multiple modes with a single line like

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    

    And if you are working in an activity you can just adjust it in your android manifest file for each activity

    <activity
        android:name=".SomeActivity"
        android:windowSoftInputMode="adjustResize" />
    

    or
    android:windowSoftInputMode="adjustPan|adjustResize"
    Check which settings suits your needs better between adjustPan, adjustUnspesified, adjustResize or some other soft input options.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search