windowsoftinputmode How to get height of keyboard when activity created in android




how to check soft keyboard is open or not in android (4)

As far as I know, there's no perfect solution that will work all the time. I have seen many people use ViewTreeObserver to get the keyboard height, although this would not work sometimes, it's better than nothing. Try it out.

I wrote a Input Method Editor a few years ago. As I remember, there is no function called "getCurrentHeight" exposed from the IME classes. So don't go this way.

And please note that nowadays many IME support to change keyboard height at runtime(when it's shown on screen), maybe you would have to take this into consideration.

I want to auto display a listview with same height of keyboard of device when user goes to that particular activity. For this, I am calling three methods that are, showKeyboard(), getKeyboardHeight() and then hideKeyboard() and then giving height to that listview and showing that listview. But the problem is once I call showKeyboard(), calculate height and then hideKeyboard(), the keyboard doesn't hide and remains visible. Also I'm getting height as 0. I cannot display that listView. Is there any another process to get height of keyboard or any correction in below code ? See the code below :

showKeyboard() method -

private void showKeyboard() {
             InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
             keyboard.showSoftInput(editChatBox, 0);
        }

getKeyboardHeight() method -

public int getKeyboardHeight() {
          final View rootview = this.getWindow().getDecorView();
           linearChatLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    public void onGlobalLayout() {
                        Rect r = new Rect();
                        rootview.getWindowVisibleDisplayFrame(r);
                        int screenHeight = rootview.getRootView().getHeight();
                        int newHeight = screenHeight - (r.bottom - r.top);
                        if (newHeight > heightOfKeyboard) {
                            heightOfKeyboard = screenHeight
                                    - (r.bottom - r.top);
                            // heightOfKeyboard = heightDiff;
                        }

                        Log.d("Keyboard Size", "Size: " + heightOfKeyboard);
                    }
                });
        return heightOfKeyboard;
    }

hideKeyboard() method -

private void hidekeyBoard() {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editChatBox.getWindowToken(), 0);
}

Inside onCreate() method -

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_new_layout);

   ArrayAdapter<String> chatQueAdapter = new ArrayAdapter<>(this,
            R.layout.chat_que_row, R.id.textChatQue, queArrays);
    myListView.setAdapter(chatQueAdapter);

        showKeyboard();
        heightOfKeyboard = getKeyboardHeight();
        hidekeyBoard();
        myListView.getLayoutParams().height = heightOfKeyboard;
        myListView.setVisibility(View.VISIBLE);
    }


i have figured out height by using custom pass then calculate it

private int keyboardHeight;

// passed here 230dp
final float popUpheight = getResources().getDimension(
            R.dimen.keyboard_height);
    changeKeyboardHeight((int) popUpheight);

// call this where you want height of keyboard
checkKeyboardHeight();

int previousHeightDiffrence = 0;

private void checkKeyboardHeight() {

    rootlayout.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    rootlayout.getWindowVisibleDisplayFrame(r);
                    int screenHeight = rootlayout.getRootView()
                            .getHeight();
                    int heightDifference = screenHeight - (r.bottom);

                    if (Math.abs(previousHeightDiffrence - heightDifference) > 50) {
                        popupWindow.dismiss();
                        imageEmoji
                                .setImageResource(R.drawable.emoji_btn_normal);
                    }

                    previousHeightDiffrence = heightDifference;
                    if (heightDifference > 100) {
                        isKeyBoardVisible = true;
                        changeKeyboardHeight(heightDifference);
                    } else {
                        isKeyBoardVisible = false;
                    }

                }
            });
}


/**
 * change height of emoticons keyboard according to height of actual
 * keyboard
 *
 * @param height minimum height by which we can make sure actual keyboard is
 *               open or not
 */
private void changeKeyboardHeight(int height) {

    if (height > 100) {
        keyboardHeight = height;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, keyboardHeight);
        emoticonsCover.setLayoutParams(params);
    }

}

for more details checkout from here, Hope this help you


Use OnGlobalLayoutListener for getting Keyboard height or implement above code snippet

chatRootLayout is your xml root layout pass this rootLayout as parentLayout parameter in checkKeyboardHeight

    private void checkKeyboardHeight(final View parentLayout)
{
  chatRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() 
  {
        @Override
        public void onGlobalLayout() 
        {
                Rect r = new Rect();

                chatRootLayout.getWindowVisibleDisplayFrame(r);

                int screenHeight = chatRootLayout.getRootView().getHeight();
                int keyboardHeight = screenHeight - (r.bottom);

                if (previousHeightDiffrence - keyboardHeight > 50) 
                {                           
                    // Do some stuff here
                }

                previousHeightDiffrence = keyboardHeight;
                if (keyboardHeight> 100) 
                {
                    isKeyBoardVisible = true;
                    changeKeyboardHeight(keyboardHeight);
                } 
                else
                {
                    isKeyBoardVisible = false;
                }
            }
    });
}

keyboardHeight() Method :-

   private void changeKeyboardHeight(int height) 
{
    if (height > 100) 
    {
            keyboardHeight = height;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, keyboardHeight);
            yourLayout.setLayoutParams(params);
    }
}




keyboard