How to check visibility of software keyboard in Android?

前端 未结 30 4409
半阙折子戏
半阙折子戏 2020-11-21 04:43

I need to do a very simple thing - find out if the software keyboard is shown. Is this possible in Android?

相关标签:
30条回答
  • 2020-11-21 05:21

    I used a slight variant of Reuban's answer, which proved to be more helpful in certain circumstances, especially with high resolution devices.

    final View activityRootView = findViewById(android.R.id.content);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightView = activityRootView.getHeight();
                    int widthView = activityRootView.getWidth();
                    if (1.0 * widthView / heightView > 3) {
                        //Make changes for Keyboard not visible
                    } else {
                        //Make changes for keyboard visible
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-21 05:21

    i think this method will help you to find out is keybord is visible or not.

     public Boolean isSoftKeyBoardVisible(){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    
        if (imm.isAcceptingText()) {
            Log.d(TAG,"Software Keyboard was shown");
            return true;
        } else {
            Log.d(TAG,"Software Keyboard was not shown");
            return false;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 05:22

    I found that a combination of @Reuben_Scratton's method along with @Yogesh's method seems to work best. Combining their methods would yield something like this:

    final View activityRootView = findViewById(R.id.activityRoot);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        if (getResources().getConfiguration().keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) { // Check if keyboard is not hidden
           // ... do something here
        }
      }
    });
    
    0 讨论(0)
  • 2020-11-21 05:23

    I used a little time to figure this out... I ran it some CastExceptions, but figured out that you can replace you LinearLayout in the layout.xml with the name of the class.

    Like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llMaster">
    
    <com.ourshoppingnote.RelativeLayoutThatDetectsSoftKeyboard android:background="@drawable/metal_background"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/rlMaster" >
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="1dip" android:background="@drawable/line"></LinearLayout>
    
              ....
    
    </com.ourshoppingnote.RelativeLayoutThatDetectsSoftKeyboard>    
    
    
    </LinearLayout>
    

    That way you do not run into any cast issues.

    ... and if you don't want to do this on every page, I recommend that you use "MasterPage in Android". See the link here: http://jnastase.alner.net/archive/2011/01/08/ldquomaster-pagesrdquo-in-android.aspx

    0 讨论(0)
  • 2020-11-21 05:24

    you can try this, work great for me:

    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    
    if (imm.isAcceptingText()) {
        //Software Keyboard was shown..
    } else {
        //Software Keyboard was not shown..
    }
    
    0 讨论(0)
  • 2020-11-21 05:28

    Here's my solution, and it works. Instead of looking for pixel size just check that the height of the content view has changed or not:

    // Scroll to the latest comment whenever the keyboard is shown
    commentsContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
            private int oldHeight;
    
            @Override
            public void onGlobalLayout() {
                int newHeight = commentsContent.getMeasuredHeight();
                if (newHeight < oldHeight) {
                    // Check for the keyboard showing in case the height difference
                    // is a result of orientation change
                    if (isSoftKeyboardShowing(CommentsActivity.this)) {
                        // Keyboard is showing so scroll to the latest comment
                        scrollToLatestComment();
                    }
                }
                oldHeight = newHeight;
            }
    
        });
    
    
    public static boolean isSoftKeyboardShowing(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        return inputMethodManager.isActive();
    }
    
    0 讨论(0)
提交回复
热议问题