How to make some controls move up when the soft keyboard pops up

When I wrote the registration page before, my classmates gave me a suggestion. When the soft keyboard pops up, the title "registration" in the upper left corner doesn't move, and the middle content moves up. The effect is like this


After consulting data and practice, the solution is as follows

1. Set the page soft keyboard mode first, so that the layout height will reduce the height of the soft keyboard after each pop-up of the soft keyboard

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

2. Find out the content view, monitor and change the content view every time the layout is soft keyboard pop-up to reduce the height

mVContent = findViewById(android.R.id.content);

ViewTreeObserver.OnGlobalLayoutListener mLsnrLayout =
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout () {

                    Rect rect = new Rect();
                    mVContent.getWindowVisibleDisplayFrame(rect);
                    int height = rect.height();

                    boolean isImmShow = height < UcApp.sHeightPx;
                    if ( isImmShow != mIsImmShow ) {
                        mIsImmShow = isImmShow;

                        mUbtnCommit.setVisibility(isImmShow ? View.INVISIBLE : View.VISIBLE);
                    }
                }
            };

mVContent.getViewTreeObserver().addOnGlobalLayoutListener(mLsnrLayout);

The XML format is as follows: the title is added on the base class

    <!--Registration layout-->
    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:visibility="visible">
public void setTitle (String txt) {

    if ( null == mTvTitle ) {

        mTvTitle = new TextView(UcApp.sCtx);
        ((FrameLayout) getWindow().getDecorView()).addView(mTvTitle);
        ViewUtils.inst(mTvTitle)
                .setMargins(119, 54, 0, 0)
                .setTextSize(42);
        mTvTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTvTitle.setTextColor(ComUtils.getClr(R.color.white_f2f2f2_60));
    }

    mTvTitle.setText(txt);
}

Just set margin

Because the content layout is vertically centered and the title is written by margin, the content moving title will not move after the layout height changes.

I hope this article can help you, don't spray~

Tags: Android xml

Posted on Wed, 13 Nov 2019 13:46:10 -0500 by phpyoungdeveloper