Detect visibility and height of soft input when using full screen mode

I read several posts of this problem and the solution was to use OnGlobalLayoutListener. The problem here is that my application declared the following in the manifest:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Therefore, setting the input mode does not work, and I cannot use OnGlobalLayoutListener

How to get the event that the soft input is visible / gone, and I would also need to get the height of the input, as well as adjust the current layout bottomMargin.

Thank.

+5
source share
3 answers

I don't know how you use it OnGlobalLayoutListener, but it works fine in my code below:

        final View activityRootView = findViewById(R.id.container);
        final ViewTreeObserver observer = activityRootView
                .getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                // r will be populated with the coordinates of your view
                // that area still visible.
                activityRootView.getWindowVisibleDisplayFrame(r);
                int heightDiff = activityRootView.getRootView().getHeight()
                        - (r.bottom - r.top);
                Toast.makeText(TestActivity.this, String.valueOf(heightDiff),
                        Toast.LENGTH_SHORT).show();

            }
        });

This code is higher in onResume()my TestActivity with the theme@android:style/Theme.NoTitleBar.Fullscreen

layout file for presentation:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aaa" />

</LinearLayout>

:

<activity
    android:name=".ui.activity.TestActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
+1

softInput. :

public boolean isKeyBoardVisible;
    int previousHeightDiffrence = 0;

    public void checkKeyboardHeight(final View parentLayout) {

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

                    @Override
                    public void onGlobalLayout() {

                        Rect r = new Rect();
                        parentLayout.getWindowVisibleDisplayFrame(r);

                        int screenHeight = parentLayout.getRootView()
                                .getHeight();
                        int heightDifference = screenHeight - (r.bottom);

                        if (previousHeightDiffrence - heightDifference > 50) {

                            // Keyboard is not visible
                        }
                        if (previousHeightDiffrence - heightDifference < -50) {

                            // Keyboard Visible
                        }

                        previousHeightDiffrence = heightDifference;
                        if (heightDifference > 100) {

                            isKeyBoardVisible = true;

                        } else {

                            isKeyBoardVisible = false;

                        }

                    }
                });

    } 

parentLayout - heightDifference - , Visible.....

0

Try the following:

Markup:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <MyView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

Then run MyView

public class MyView extends View
{
     public static int ScreenHeight;

     @Override
     public function onDraw(Canvas canvas){
          if(ScreenHeight != canvas.getHeight()){
              // this is your event
          } 
          ScreenHeight = canvas.getHeight();

     }
}

Now ScreenHeight- this is what you are looking for.

0
source

All Articles