Home » Java » java – How to prevent bottom views pushing up when keyboard opens-Exceptionshub

java – How to prevent bottom views pushing up when keyboard opens-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

There are two edittext on a screen and a button on bottom of the screen, the problem is when I click on edittext and keyboard open ups the button at the bottom appears above the keyboard, I want bottom buton to be at its original place rather than moving up with keyboard. the button is bottom aligned

Thanks

P.S:
I have tried adjust pan, resize etc nothing working

<activity
        android:screenOrientation="portrait"
        android:name=".activities.MainActivity"
        android:windowSoftInputMode="stateUnchanged|adjustResize"
        android:theme="@style/AppTheme"
     />

XML:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/text_white"
    android:orientation="vertical"
    android:paddingTop="@dimen/_20sdp"
    android:elevation="5dp"
    tools:context=".fragments.login.LoginFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_welcome"
        android:layout_marginRight="@dimen/_40sdp"
        android:layout_marginStart="@dimen/_20sdp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:maxLines="2"
        style="@style/headingText"
        android:text="Welcome to the World \nof PACKR" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:id="@+id/tv_continue"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@+id/tv_welcome"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="@dimen/_20sdp"
        android:layout_marginStart="@dimen/_20sdp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:text="Sign in to continue" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/_20sdp"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:layout_marginStart="@dimen/_20sdp"
        android:layout_marginEnd="@dimen/_20sdp"
        app:layout_constraintTop_toBottomOf="@+id/tv_continue"
        android:id="@+id/phone_layout"
        android:orientation="horizontal">

        <com.hbb20.CountryCodePicker
            android:layout_marginEnd="@dimen/_10sdp"
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:textSize="@dimen/_12sdp"
            android:id="@+id/countryCode"
            style="@style/smallText"
            android:background="@drawable/edittext_background"
            android:layout_height="match_parent" />

        <EditText
            android:layout_width="0dp"
            android:layout_weight="0.6"
            android:hint="Phone Number"
            android:inputType="number"
            style="@style/smallText"
            android:id="@+id/et_phone"
            android:maxLength="11"
            android:padding="@dimen/_10sdp"
            android:drawablePadding="10dp"
            android:drawableStart="@drawable/ic_phone"
            android:background="@drawable/edittext_background"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <EditText
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/_10sdp"
        android:hint="Password"
        android:singleLine="true"
        android:drawablePadding="10dp"
        android:layout_marginEnd="@dimen/_20sdp"
        android:layout_marginStart="@dimen/_20sdp"
        app:layout_constraintTop_toBottomOf="@+id/phone_layout"
        app:layout_constraintLeft_toLeftOf="parent"
        android:padding="@dimen/_10sdp"
        style="@style/smallText"
        android:inputType="textPassword"
        android:id="@+id/et_password"
        android:drawableStart="@drawable/ic_lock"
        android:background="@drawable/edittext_background"
        android:layout_height="wrap_content" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:id="@+id/forgot_layout"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="@dimen/_15sdp"
        android:layout_height="wrap_content">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/img_"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="@+id/forgot_pass"
            app:layout_constraintLeft_toLeftOf="@+id/forgot_pass"
            android:src="@drawable/forgot_icon"
            android:layout_marginEnd="@dimen/_20sdp"
            android:layout_marginStart="@dimen/_20sdp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            style="@style/smallText"
            android:layout_marginEnd="@dimen/_20sdp"
            android:layout_marginStart="@dimen/_20sdp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/img_"
            android:layout_gravity="right"
            android:padding="@dimen/_5sdp"
            android:textColor="@color/grey_border"
            android:id="@+id/forgot_pass"
            android:layout_marginTop="8dp" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_signin"
        android:textAppearance="@style/textOnButton"
        android:text="Login"
        app:layout_constraintTop_toBottomOf="@+id/et_password"
        android:layout_marginEnd="@dimen/_20sdp"
        android:layout_marginStart="@dimen/_20sdp"
        android:layout_marginTop="@dimen/_30sdp"
        android:paddingLeft="5dp"
        />


        <ProgressBar
            android:id="@+id/pb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="50dp"
            android:minWidth="50dp"
            android:gravity="center"
            android:indeterminate="true"
            android:elevation="10dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/orange"
            />



</androidx.constraintlayout.widget.ConstraintLayout>
How to&Answers:

Update following in the activity tag in your manifest:

android:windowSoftInputMode=”adjustNothing”

Answer:

Set your XML file like this (make changes as per your need)

    <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    android:paddingTop="20dp"
    android:elevation="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_welcome"
        android:layout_marginRight="40dp"
        android:layout_marginStart="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:maxLines="2"
        style="App Wep"
        android:text="Welcome to the World \nof PACKR" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:id="@+id/tv_continue"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@+id/tv_welcome"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:text="Sign in to continue" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintTop_toBottomOf="@+id/tv_continue"
        android:id="@+id/phone_layout"
        android:orientation="horizontal">

        <com.hbb20.CountryCodePicker
            android:layout_marginEnd="10dp"
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:textSize="12dp"
            android:id="@+id/countryCode"
            android:layout_height="match_parent" />

        <EditText
            android:layout_width="0dp"
            android:layout_weight="0.6"
            android:hint="Phone Number"
            android:inputType="number"
            android:id="@+id/et_phone"
            android:maxLength="11"
            android:padding="10dp"
            android:drawablePadding="10dp"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <EditText
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:hint="Password"
        android:singleLine="true"
        android:drawablePadding="10dp"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        app:layout_constraintTop_toBottomOf="@+id/phone_layout"
        app:layout_constraintLeft_toLeftOf="parent"
        android:padding="10dp"
        android:inputType="textPassword"
        android:id="@+id/et_password"
        android:layout_height="wrap_content" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:id="@+id/forgot_layout"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="15dp"
        android:layout_height="wrap_content">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/img_"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="@+id/forgot_pass"
            app:layout_constraintLeft_toLeftOf="@+id/forgot_pass"
            android:layout_marginEnd="20dp"
            android:layout_marginStart="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:layout_marginEnd="20dp"
            android:layout_marginStart="20dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/img_"
            android:layout_gravity="right"
            android:padding="5dp"
            android:id="@+id/forgot_pass"
            android:layout_marginTop="8dp" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_signin"
        android:text="Login"
        app:layout_constraintTop_toBottomOf="@+id/et_password"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="30dp"
        android:paddingLeft="5dp"
        />


    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:gravity="center"
        android:indeterminate="true"
        android:elevation="10dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:indeterminateTintMode="src_atop"
        />



</androidx.constraintlayout.widget.ConstraintLayout>

And Manifest as this(Example)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Remove WindowSoftInput mode.