Getting the dimensions of the soft keyboard

后端 未结 11 734
故里飘歌
故里飘歌 2020-11-27 14:56

Is there a way to know the size of the keyboard that is shown in the screen?

I am using Cocos2dx for programming, but I want to know the height of the keyboard shown

相关标签:
11条回答
  • 2020-11-27 15:03

    If you want to calculate the Virtual Keyboard height while your activity does not change in size (adjustPan) then you can use this sample:

    https://github.com/siebeprojects/samples-keyboardheight

    It uses a hidden window in order to calculate the height difference between the window and the root view of the activity.

    0 讨论(0)
  • 2020-11-27 15:04

    Complete answer & worked perfectly for me:

      Rect r = new Rect();
      View rootview = this.getWindow().getDecorView(); // this = activity
      rootview.getWindowVisibleDisplayFrame(r);
      int keyboardHeight = rootview.getHeight() - r.bottom;
    
    0 讨论(0)
  • 2020-11-27 15:07
    Rect r = new Rect();
    View rootview = this.getWindow().getDecorView(); // this = activity
    rootview.getWindowVisibleDisplayFrame(r);
    

    Result of this is the amount of space your application uses on screen (works even when activity is not resized). Obviously remaining screen space will be used by the keyboard ( if its visible)

    Found id up here: https://github.com/freshplanet/ANE-KeyboardSize/blob/master/android/src/com/freshplanet/ane/KeyboardSize/getKeyboardY.java

    0 讨论(0)
  • 2020-11-27 15:08

    After hours of searching I found a solution if you want to set windowSoftInput="adjustPan"

    Here is the code snippet:

        final View root  = findViewById(android.R.id.content);
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        Rect r = new Rect();
        {
            root.getWindowVisibleDisplayFrame(r);
        }
        @Override
        public void onGlobalLayout() {
            Rect r2 = new Rect();
            root.getWindowVisibleDisplayFrame(r2);
            int keyboardHeight = r.height() - r2.height();
            if (keyboardHeight > 100) {
                root.scrollTo(0, keyboardHeight);
            }
            else {
                root.scrollTo(0, 0);
            }
        }
    });
    

    In this code, after I found the keyboard height I scroll the view up to not covered by the keyboard which is the main reason for finding the keyboard height.

    According to the docs :

    void getWindowVisibleDisplayFrame(Rect outRect) : Retrieve the overall visible display size in which the window this view is attached to has been positioned in.

    0 讨论(0)
  • 2020-11-27 15:09

    You can't tell. No, really: you simply can't tell.

    The keyboard does not need to be any particular shape. It does not have to be placed at the bottom of the screen (many of the most popular options are not), it does not have to keep its current size when you change text fields (almost none do depending on the flags). It does not even have to be rectangular. It may also just take over the entire screen.

    0 讨论(0)
  • 2020-11-27 15:12

    I know this is an old post, but I noticed that the chosen solution for me did not work on all devices. There seemed to be a discrepancy and so I implemented this and it seems to be a catch all:

            final int[] discrepancy = new int[1];
            discrepancy[0] = 0;
    
            // this gets the height of the keyboard
            content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    View rootview = activity.getWindow().getDecorView(); // this = activity
                    rootview.getWindowVisibleDisplayFrame(r);
    
                    int screen_height = rootview.getRootView().getHeight();
                    int keyboard_height = screen_height - (r.bottom + r.top) - discrepancy[0];
    
                    if (discrepancy[0] == 0) {
                        discrepancy[0] = keyboard_height;
                        if (keyboard_height == 0) discrepancy[0] = 1;
                    }
    
                    int margin_bottom = keyboard_height + Helper.getDp(10, activity);
    
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) carousel_container.getLayoutParams();
                    params.setMargins(0, 0, 0, margin_bottom);
    
                    //boolean visible = heightDiff > screenHeight / 3;
                }
            });
    

    When the listener is first called it measures the screen without a keyboard and if there is a discrepancy I account for it the next time around. If there is no discrepancy I set the discrepancy to 1 just so it is no longer 0.

    0 讨论(0)
提交回复
热议问题