How to adjust the screen according to the Android virtual keyboard?

1

I would like to know how to resize the screen according to the display of the Android Virtual keyboard, style the Evernote home screen, that when the keyboard appears the image decreases and when the keyboard turns off, the upper image expands again.

My code looks like this:

In activity_login.xml there is AppBarLayout, which I want to expand or collapse, 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'm working with Fragments, and everything is correct, and I put the logic in the fragment because it will be displayed on the whole screen if you activate the keyboard and put it that 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 I activate the keyboard setExpended does not run, and does not point to any error, but when I minimize the login screen on android and then maximize, it does what I want.

How to solve this?

    
asked by anonymous 06.11.2015 / 15:48

1 answer

1

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

android:windowSoftInputMode="adjustResize"
    
06.11.2015 / 17:49