adnx adnx - 2 days ago 3
Android Question

How to move views up when keyboard appears inside ConstraintLayout

Right now, I have one

Button
and one
EditText
view inside
ConstraintLayout
XML file. Need to move views up when user is typing inside
EditText
.

Manifest
windowSoftInputMode
is not working, so I think I need to work with constraints. Does anyone have clue how to solve this?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"

android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:srcCompat="@drawable/sx_landing_bg"
android:id="@+id/fpp_background"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
tools:ignore="ContentDescription"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="1.0" />


<android.support.v7.widget.AppCompatButton
android:layout_height="0dp"
android:layout_width="0dp"
android:text="@string/forgot_password_reset_password"
android:id="@+id/btn_login"
android:textColor="@color/colorDarkGray"
android:theme="@style/WhiteButtonStyle"
android:backgroundTint="@color/colorPrimaryDark"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="@+id/gl_vl_16d"
app:layout_constraintRight_toLeftOf="@+id/gl_vr_16d"
app:layout_constraintBottom_toTopOf="@+id/gl_vh_80p"
app:layout_constraintTop_toTopOf="@+id/gl_vh_70p"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
android:gravity="center_vertical|center_horizontal|center" />

<android.support.constraint.Guideline
android:layout_width="411dp"
android:layout_height="wrap_content"
android:id="@+id/gl_vh_80p"
android:orientation="horizontal"
tools:layout_editor_absoluteY="541dp"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintGuide_percent="0.7922078"
/>

<android.support.constraint.Guideline
android:layout_width="411dp"
android:layout_height="wrap_content"
android:id="@+id/gl_vh_70p"
android:orientation="horizontal"
tools:layout_editor_absoluteY="478dp"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintGuide_percent="0.7"
/>

<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="683dp"
android:id="@+id/gl_vr_16d"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="395dp"
app:layout_constraintGuide_end="16dp"
/>

<android.support.constraint.Guideline
android:layout_width="0dp"
android:layout_height="683dp"
android:id="@+id/gl_vl_16d"
app:layout_constraintGuide_begin="16dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="16dp"
/>

<android.support.constraint.Guideline
android:layout_width="0dp"
android:layout_height="683dp"
android:id="@+id/guideline3"
app:layout_constraintGuide_begin="387dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="387dp" />

<android.support.constraint.Guideline
android:layout_width="0dp"
android:layout_height="683dp"
android:id="@+id/guideline4"
app:layout_constraintGuide_begin="24dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="24dp" />

<android.support.constraint.Guideline
android:layout_width="411dp"
android:layout_height="wrap_content"
android:id="@+id/guideline9"
app:layout_constraintGuide_begin="372dp"
android:orientation="horizontal"
tools:layout_editor_absoluteY="372dp"
tools:layout_editor_absoluteX="0dp" />

<android.support.constraint.Guideline
android:layout_width="411dp"
android:layout_height="wrap_content"
android:id="@+id/guideline"
app:layout_constraintGuide_begin="431dp"
android:orientation="horizontal"
tools:layout_editor_absoluteY="431dp"
tools:layout_editor_absoluteX="0dp" />

<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText2"
android:textColor="@color/button_text_color"
android:theme="@style/WhiteButtonStyle"
app:layout_constraintLeft_toLeftOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/guideline9"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="1.0"
android:hint="Enter email or ussername"
style="@style/RobotoEditTextStyle"
android:textSize="14sp"

app:layout_constraintRight_toLeftOf="@+id/gl_vr_16d"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
android:layout_marginBottom="8dp"
android:textColorHint="#bababa" />


</android.support.constraint.ConstraintLayout>

Answer

Done by setting views programmatically at runtime and checking when keyboard appears.

   ConstraintLayout.LayoutParams constraint = (ConstraintLayout.LayoutParams)login.getLayoutParams();
    constraint.topToBottom = shrinked ? R.id.gl_h_850p: R.id.gl_h_550p;
    constraint.bottomToTop = shrinked ? R.id.gl_h_999p: R.id.gl_h_650p;
    login.setLayoutParams(constraint);
Comments