How do I detect if software keyboard is visible on Android Device or not?

前端 未结 30 1352
情书的邮戳
情书的邮戳 2020-11-22 10:59

Is there a way in Android to detect if the software (a.k.a. \"soft\") keyboard is visible on screen?

30条回答
  •  隐瞒了意图╮
    2020-11-22 11:53

    As you might know android Software keyboard will be visible only when there is a possible event of typing. In other words Keyboard get visible only when EditText is focused. that means you can get weather the Keyboard is visible or not by using OnFocusChangeListener.

    //Declare this Globally
    
    public boolean isKeyBoardVisible = false;
    
    //In OnCreate *[For Activity]*, OnCreateView *[For Fragment]*
    
    text_send.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                isKeyBoardVisible = true;
            else
                isKeyBoardVisible = false;
        }
    });
    

    Now you can use isKeyBoardVisible variable anywhere in the class to get weather the keyboard is Open or Not. It worked well for me.

    Note: This process doesn't work when the Keyboard is opened programmatically using InputMethodManager because that doesn't invoke OnFocusChangeListener.

提交回复
热议问题