5

I've read multiple posts of this issue and the solution was to use OnGlobalLayoutListener. Problem here is that my application has declared following in manifest:

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

So the inputmode adjust doesn't work and I cant use OnGlobalLayoutListener

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

Thanks.

Niko
  • 8,093
  • 5
  • 49
  • 85

3 Answers3

1

I don't know how you use 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();

            }
        });

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

layout file for the view:

<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>

The activity config in menifest:

<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>
Bolton
  • 2,226
  • 3
  • 25
  • 29
  • Can you post the xml also, my application has only one Activity and everything else are Fragments. – Niko May 14 '13 at 05:23
  • http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible This clearly states that it doesn't work and there are also bugs for Google about it. – Niko May 14 '13 at 05:30
  • And due to that post this seems to be a duplicate. – Niko May 14 '13 at 05:33
  • @Niko I don't know whether I've misunderstood you, but take a look at my xml – Bolton May 14 '13 at 06:12
  • Worked for me on API19 with the following flags enabled: `WindowManager.LayoutParams.FLAG_FULLSCREEN`, `View.SYSTEM_UI_FLAG_HIDE_NAVIGATION`, `View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY`, `WindowManager.LayoutParams.FLAG_SECURE` (required for us, should not affect this answer), and `WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED` (required for us, should not affect this answer). We actually use this to reapply the two View flags after the soft keyboard is hidden (otherwise the navigation remains visible). – claytond Aug 12 '15 at 17:19
0

I use this for detecting visibilty of softInput. Try this :

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;

                        }

                    }
                });

    } 

here parentLayout is top most layout of my activity and heightDifference is height of my keyboard if keyboard is Visible.....

Chirag Jain
  • 1,612
  • 13
  • 20
  • Not working atleast with my api level which is 8. And Dianne Hackborn posted answer to this: https://groups.google.com/forum/?fromgroups=#!msg/android-developers/T193nVgXDSA/O6UZmDqskFYJ -> It cannot work. – Niko May 15 '13 at 18:04
  • This does not answer the question. The question was about full screen mode only. – colintheshots Apr 19 '16 at 21:51
0

Try this:

Layout:

<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 Implement the 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 is what you are looking for.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • Well this has absolutely nothing to do with my question. – Niko May 17 '13 at 05:36
  • 1
    http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible this has the same issue. Window doesn't resize it's content so screen height remains the same. – Niko May 17 '13 at 09:47