Android Edit Text Covered By Keyboard on Second Tap

So, I have an EditText that is not covered by the keyboard for the first time. Then, when you close the keyboard and click on edittext again, it closes the edittext.

I spent hours studying this problem and came to the conclusion that this has something to do with these two properties of the editing text.

android:inputType="number"
android:gravity="center"

If I delete one of them, then configurePan (as shown in my manifest) works all the time, as promised. This seems to be an Android bug. But I need both of these lines in my text editor. What is the best way to solve this problem?

Here is a slightly compressed version of xml:

<RelativeLayout 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:background="@color/colorPrimary"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">


<LinearLayout
    android:id="@+id/buttons_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:weightSum="5">

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/generate_button"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:layout_marginBottom="@dimen/button_margin"
        android:layout_marginEnd="0dp"
        android:layout_marginLeft="@dimen/button_margin"
        android:layout_marginRight="0dp"
        android:layout_marginStart="@dimen/button_margin"
        android:layout_marginTop="@dimen/button_margin"
        android:layout_weight="1"
        android:background="@color/buttonColor"
        android:elevation="@dimen/button_elevation"
        android:foreground="?attr/selectableItemBackground"
        android:text="@string/generate"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/generate_button_title_size"
        android:textStyle="bold" />

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/copy_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/button_margin"
        android:layout_weight="4"
        android:adjustViewBounds="true"
        android:background="@color/buttonColor"
        android:elevation="@dimen/button_elevation"
        android:foreground="?attr/selectableItemBackground"
        android:padding="@dimen/button_margin"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_copy"
        android:tint="@color/colorPrimary" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/buttons_layout"
    android:orientation="vertical"
    android:weightSum="10">

    <GridLayout
        android:id="@+id/grid_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="6"
        android:animateLayoutChanges="true">
    </GridLayout>


    <TextView
        android:id="@+id/total_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center"
        android:text="11"
        android:textSize="@dimen/toolbar_title_size"
        android:textStyle="bold" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:textColorHint="@color/textColorHint">

        <EditText
            android:id="@+id/num_rolls_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="@string/number_of_rolls"
            android:inputType="number"
            android:maxLength="@integer/edit_text_max_length"
            android:maxLines="1"
            android:text="4"
            android:textColor="@color/textColor"
            android:textSize="@dimen/edit_text_text_size"/>

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

</LinearLayout>

Here's what it should look like every time ... enter image description here

Here's what it looks like at the second touch at the moment (keyboard covers edittext) ... enter image description here

EDIT: , Android 7.0. , - . , ​​ - ?

, android: windowSoftInputMode = "adjustPan | stateHidden" , , , .

+6
7

.

 <activity android:name=".Views.UI.MainActivity"
        android:windowSoftInputMode="stateHidden|adjustResize">
+4

adjustPan adjustResize , .

(adjustPan), , , , , .

, / , ScrollView RelativeLayout. , adjustResize , "" .

, !

+3

"Dice" , , editText, "Dice" , Title editText "Dice" ?

, scrollView , AlignParentBottom, layoutBelow?

+2

, XML, , Android-. <activity android:name=".activity.ActivityProductDetail" android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />

+1

RelativeLayout alignParentBottom, , . scrollview LinearLayout, , .

adjustPan. - :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- grid view + textview + textEdit + your buttons at the end -->

</LinearLayout>

+1

ScrollView onGloablLayout() . rootview > 100 , , , , EditText, .

final View layout = findViewById(R.id.layoutparentID);
    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = layout.getRootView().getHeight() - layout.getHeight();
            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

                final Runnable r = new Runnable() {
                    public void run() {
                        scrollView.smoothScrollTo(0, editText.getBottom());
                    }
                };
                activityRootView.getHandler().postDelayed(r, 100);
            }
        }
    });
+1
source

Check this code by hiding the soft keyboard

 try {
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
+1
source

All Articles