android monitors pop-up and hiding of soft keyboard

Keywords: Android Windows xml encoding

ScrollView in the layout file, EditView in the ScrollView, a control at the bottom of the layout (see the layout code below). As soon as the program starts, EditView gets the focus and pops up the soft keyboard. It doesn't feel good to top the control at the bottom, so I want to listen to the soft keyboard pop up, hide the bottom control at this time, and show the bottom when the soft keyboard is hidden. Part control.
Initial:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="com.test.myapplication.MainActivity">

    <TextView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <LinearLayout
        android:id="@+id/lin"
        android:background="#0000ff"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    <TextView
        android:layout_margin="10dp"
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
</LinearLayout>

The android api provides ways to make soft keyboards pop up and hide, such as

if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
        {
         //Hide Soft Keyboard
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        }

However, there is no way to monitor the pop-up and hiding of the soft keyboard.
Because pop-up and hidden soft keyboards are bound to cause layout changes, there are two solutions to monitor the layout changes and then calculate the offset, which can calculate whether to display or hide.
1. Customize View and modify OnLayout() methods, such as

public class ResizeLayout extends LinearLayout {
    private InputListener mListener;

    public interface InputListener {
        void OnInputListener(boolean isHideInput);
    }

    public void setOnResizeListener(InputListener l) {
        mListener = l;
    }

    public ResizeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private boolean mHasInit = false;
    private boolean mHasKeyboard = false;
    private int mHeight;

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        super.onLayout(changed, l, t, r, b);
        if (!mHasInit) {
            mHasInit = true;
            mHeight = b;
            System.out.println("mHeight= " + b);
        }
        else {
            mHeight = mHeight < b ? b : mHeight;
        }

        if (mHasInit && mHeight > b) { // MHeight represents the true height of the keyboard and B represents the height mHeight > b in the window.
            mHasKeyboard = true;
            mListener.OnInputListener(false);
        }
        if (mHasInit && mHasKeyboard && mHeight == b) { // mHeight = b
            mHasKeyboard = false;
            mListener.OnInputListener(true);
        }
    }

2. Getting the Height Change of ViewGroup in activity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*// Hide the title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hide the status bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
        setContentView(R.layout.activity_main);
        final LinearLayout lin = (LinearLayout) findViewById(R.id.lin);
        final TextView txt = (TextView) findViewById(R.id.txt);

        lin.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                lin.getWindowVisibleDisplayFrame(rect);
                int rootInvisibleHeight = lin.getRootView().getHeight() - rect.bottom;
                Log.d(TAG, "lin.getRootView().getHeight()=" + lin.getRootView().getHeight() + ",rect.bottom=" + rect.bottom + ",rootInvisibleHeight=" + rootInvisibleHeight);
                if (rootInvisibleHeight <= 100) {
                //Soft keyboard hidden
                    txt.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            txt.setVisibility(View.VISIBLE);
                        }
                    },100);
                } else {
                    //// Soft keyboard pops up
                    txt.setVisibility(View.GONE);
                }
            }
        });
    }

Off-topic topic: The test found that by setting full screen getWindow (). setFlags (Windows Manager. LayoutParams. FLAG_FULLSCREEN,
Windows Manager. LayoutParams. FLAG_FULLSCREEN; can achieve the same goal, but the full screen is against my original intention. Personally, I recommend the second method, because when a customer's device opens the fingerprint identification album lock, the first method is not good.
In the process of searching for information, we can see that some developers want the top of the bottom control when the soft keyboard pops up. The method is the same as above.

Posted by konigwolf on Mon, 01 Apr 2019 23:45:29 -0700