ScrollView doesn't scroll if soft keyboard is visible

I use a simple layout for the login screen, which contains the top panel and the necessary elements for logging in or going to registration activity.

All the elements are great on the screen until a virtual keyboard appears. The Android soft keyboard hides part of the form, and I want to use ScrollView so that the user can scroll and see all the elements.

But even with ScrollView I can’t scroll to see the bottom of the page:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFCB05" android:orientation="vertical" android:isScrollContainer="true" android:gravity="top" > <include android:id="@+id/include1" android:layout_width="fill_parent" android:layout_height="50dip" layout="@layout/actionbar_layout" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" android:gravity="center_vertical" android:layout_below="@+id/include1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/usuario" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#000000" /> <EditText android:id="@+id/txtUsuario" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="5dp" android:ems="10" android:hint="@string/usuario" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="15dip" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="@string/senha" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#000000" /> <EditText android:id="@+id/txtSenha" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_centerVertical="true" android:layout_marginTop="5dip" android:ems="10" android:hint="@string/senha" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="right" > <Button android:id="@+id/logarBtn" android:layout_width="93dp" android:layout_height="wrap_content" android:layout_marginRight="30dip" android:layout_marginTop="15dip" android:layout_marginBottom="15dip" android:text="@string/logar" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:id="@+id/registerBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/logarBtn" android:layout_centerHorizontal="true" android:layout_marginTop="20dip" android:layout_marginBottom="60dip" android:linksClickable="true" android:text="@string/cadastrar" android:textColor="#0000CC" android:textSize="18dp" /> </LinearLayout> </LinearLayout> </RelativeLayout> </ScrollView> 

I think I could do this: Android Soft Keyboard outshines EditTexts in ScrollView , but this does not seem to be the best practice.

Does anyone know a better way to get it to scroll properly?

+6
source share
2 answers

Check out the following link:

Android: ScrollView does not scroll from keyboard

This solution worked for me.

+2
source
 public class CustomScrollView extends ScrollView { private int scrollOffset = 0; public CustomScrollView (final Context context, final AttributeSet attrs) { super(context, attrs); } public void setScrollOffset (final int scrollOffset) { this.scrollOffset = scrollOffset; } @Override protected int computeScrollDeltaToGetChildRectOnScreen (final Rect rect) { // adjust by scroll offset int scrollDelta = super.computeScrollDeltaToGetChildRectOnScreen(rect); int newScrollDelta = (int) Math.signum(scrollDelta) * (scrollDelta + this.scrollOffset); return newScrollDelta; } } 

Use this custom scroll view instead of the usual scroll. Change the value of scrollOffset to achieve the desired result (scrollOffset = 10 means that an additional 10 dp will scroll down when the soft keyboard opens). Make sure android:windowSoftInputMode="adjustResize" added to the activity android:windowSoftInputMode="adjustResize"

+1
source

Source: https://habr.com/ru/post/919338/


All Articles