简体   繁体   中英

Android how to make scrollView auto scroll to the bottom when click on editText?

I show you guy the normal view of the layout below:

Image 1

When I clicked on email editText the softkeyboard was pop up, the editText is cover by softkeyboard, it that show as picture below:

Image 2

So, I wanted to click the editText and the scrollView will auto scroll to the bottom and not cover by softkeyboard that show as picture below:

Image 3

mylayout.xml code below:

<?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"
android:background="@color/bg_account_light_brown"
android:clickable="true">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/scroll"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/btn_login">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="1.0">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/bg_carousel_01" />

        <EditText
            android:id="@+id/i_email"
            android:layout_width="320dp"
            android:layout_height="49dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/rounded_shape_input"
            android:ems="10"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.493"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView" />


        <TextView
            android:id="@+id/textViewSignUp"
            android:layout_width="284dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="300dp"
            android:fontFamily="@font/lato_bold"
            android:gravity="center"
            android:text="LOGIN"
            android:textColor="@color/white"
            android:textSize="70sp"
            android:textStyle="bold|bold"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView" />

        <EditText
            android:id="@+id/i_password"
            android:layout_width="320dp"
            android:layout_height="49dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/rounded_shape_input"
            android:ems="10"
            android:hint="Password"
            android:inputType="textPassword"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.493"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/i_email"
            app:layout_constraintVertical_bias="0.52" />

    </android.support.constraint.ConstraintLayout>
</ScrollView>

<Button
    android:id="@+id/btn_login"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/rounded_shape2"
    android:clickable="true"
    android:text="Login"
    android:textColor="#FFFFFF"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.973" />

</android.support.constraint.ConstraintLayout>

Try this use make your ScrollView android:fillViewport="true"

android:fillViewport="true"

Defines whether the scrollview should stretch its content to fill the viewport.

and in your manifest file add android:windowSoftInputMode="adjustResize" in your login activity

android:windowSoftInputMode="adjustResize"

The activity's main window is always resized to make room for the soft keyboard on screen

<activity
    android:name=".activities.LoginActivity"
    android:windowSoftInputMode="adjustResize"/>

Add in Your Scrollview :

android:fillViewport="true"

And Add in Your Manifest File :

android:windowSoftInputMode="adjustResize"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM