How to adjust screen according to Android virtual keyboard?

Asked

Viewed 1,233 times

1

I would like to know how to resize the screen according to the display or not of the Android Virtual keyboard, Evernote’s home screen style, that when the keyboard appears the image decreases and when the keyboard disables, the top image expands again.

My code is as follows:

In activity_login.xml is Appbarlayout, which I want to expand or decrease and the snippet is as follows:

<android.support.design.widget.AppBarLayout
    android:id="@+id/ablLogin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

//aqui fica o ImageView

</android.support.design.widget.AppBarLayout>

I am working with Fragments, and everything is right, and I put the logic in Ragment because it will be displayed on the full screen if activates the keyboard and put this way.

Fragmentlogin:

private View view;

public View onCreateView{

view = inflater.inflate(R.layout.fragment_login, container, false);

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            AppBarLayout appBarLayout = (AppBarLayout) ((View) (view.getParent().getParent().getParent())).findViewById(R.id.ablLogin);

            Rect r = new Rect();

            view.getWindowVisibleDisplayFrame(r);
            int screenHeight = view.getRootView().getHeight();

            // r.bottom is the position above soft keypad or device button.
            // if keypad is shown, the r.bottom is smaller than that before.
            int keypadHeight = screenHeight - r.bottom;

            Log.v(tag, "view.getParent() = " + view.getParent().getParent().getParent().getClass());

            if (appBarLayout == null) {
                Log.v(tag, "appBarLayout eh null");
            } else {
                Log.v(tag, AppBarLayout.class.getName());
            }

            if(appBarLayout!=null){
                if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                    // keyboard is opened
                    Log.v(tag, "Aberto:  " + keypadHeight);
                    appBarLayout.setExpanded(false, true);
                } else {
                    // keyboard is closed
                    Log.v(tag, "Fechado " + keypadHeight);
                    appBarLayout.setExpanded(true, true);
                }
            }

        }
    });

return view;
}

What happens is that when activating the keyboard the setExpended does not run, and points no error, only when minimized the login screen on android and then maximized, it does what I want.

How to solve this?

  • So you already have the solution to maximize and minimize the image? Only it only works if you open the keyboard and exit and enter the App, right?

  • Exactly that fellow @Luiz

1 answer

1


Within your AndroidManifest.xml add the following property inside the <activity> you want to add this attribute:

android:windowSoftInputMode="adjustResize"
  • Unfortunately not solved, remains the same thing

  • Try to put android:windowSoftInputMode=adjustPan|adjustResize

Browser other questions tagged

You are not signed in. Login or sign up in order to post.