How to draw view on top of soft keyboard like WhatsApp?

后端 未结 7 872
一个人的身影
一个人的身影 2020-12-07 19:26

I want to know how it\'s possible to add View on top of Keyboard like WhatsApp and Hangout. In chat screen, they insert emoticons view on top of the opened soft

7条回答
  •  爱一瞬间的悲伤
    2020-12-07 19:48

    I recently had to implement a view which would be above a soft keyboard. @Chirag Jain's solution is almost right, but it does not count with the system buttons in the bottom of the screen! This will make the keyboard height incorrect on some devices like NEXUS 6. This solution should work across all devices:

    1) create layout which contains your view

    
    
               ....
    
        
    

    2) Bind view

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.notifications_email_settings_fragment, container, false);
    
        ButterKnife.bind(this, rootview);
    
        checkKeyboardHeight(rootview);
    

    3) keyboard check and view margin settings

    private void checkKeyboardHeight(final View parentLayout) {
    
        parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
    
                    int previousHeightDiffrence = 0;
                    int systemBarHigh = 999999;
    
                    @Override
                    public void onGlobalLayout() {
    
    
                        Rect r = new Rect();
                        parentLayout.getWindowVisibleDisplayFrame(r);
    
                        int screenHeight = parentLayout.getRootView()
                                .getHeight();
                        int keyboardHeight = screenHeight - (r.bottom);
    
                        if(systemBarHigh > keyboardHeight) {
                            systemBarHigh = keyboardHeight;
                        }
    
                        if (keyboardHeight > 250) {
    
                            int keyboardHightWithoutSystemBar = keyboardHeight - systemBarHigh;
                            // no need to update when the keyboard goes down
                            if (previousHeightDiffrence != keyboardHightWithoutSystemBar) { // if (Math.abs(previousHeightDiffrence - keyboardHeight) > 10) {
                                adjustKeyboard(keyboardHightWithoutSystemBar);
                            }
    
                            keyboardInfoContainer.setVisibility(View.VISIBLE);
                            isKeyBoardVisible = true;
                            previousHeightDiffrence = keyboardHightWithoutSystemBar;
    
                        } else {
                            isKeyBoardVisible = false;
                            if (keyboardInfoContainer != null) {
                                keyboardInfoContainer.setVisibility(View.INVISIBLE);
                            }
                        }
                    }
                });
    }
    
    private void adjustKeyboard(int keyboardHeight) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) keyboardInfoContainer.getLayoutParams();
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp.bottomMargin = keyboardHeight;
        keyboardInfoContainer.requestLayout();
    }
    

提交回复
热议问题