1

What I would like is when a user clicks on one of the edit texts for the screen to resize and scroll up so the it focuses on the that edit text.

The manifest is setup for adjust resize:

  <activity
     android:name=".ui.login.LoginActivity"
     android:label="@string/app_name"
     android:theme="@style/AppThemes"
     android:windowSoftInputMode="stateHidden|adjustResize"/>

The XML layout for the login screen:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/main_background" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <!--<LinearLayout-->
                <!--android:id="@+id/login_header"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:paddingTop="30dp"-->
                <!--android:paddingBottom="30dp"-->
                <!--android:gravity="center_horizontal"-->
                <!--android:orientation="vertical">-->

                <!--<ImageView-->
                    <!--android:id="@+id/login_fragment_logo"-->
                    <!--android:layout_width="120dp"-->
                    <!--android:layout_height="120dp"-->
                    <!--android:src="@drawable/logo_with_dropshadow" />-->

                <!--<TextView-->
                    <!--android:id="@+id/login_fragment_app_title"-->
                    <!--android:layout_width="wrap_content"-->
                    <!--android:layout_height="wrap_content"-->
                    <!--android:layout_margin="5dp"-->
                    <!--android:text="@string/digital_door_viewer"-->
                    <!--android:textAllCaps="true"-->
                    <!--android:textSize="18sp" />-->

            <!--</LinearLayout>-->

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

                <EditText
                    android:id="@+id/login_fragment_email_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="7dp"
                    android:background="@color/white"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:padding="8dp"
                    android:singleLine="true"
                    android:textSize="20sp"/>

                <EditText
                    android:id="@+id/login_fragment_password_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:padding="8dp"
                    android:singleLine="true"
                    android:textSize="20sp" />

                <Button
                    android:id="@+id/login_fragment_register_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="7dp"
                    android:layout_marginTop="10dp"
                    android:background="@android:color/transparent"
                    android:minHeight="0dp"
                    android:text="@string/register"
                    android:textAllCaps="false"
                    android:textSize="16sp" />

                <Button
                    android:id="@+id/login_fragment_forgot_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="5dp"
                    android:background="@android:color/transparent"
                    android:minHeight="0dp"
                    android:text="@string/forgot_your_password"
                    android:textAllCaps="false"
                    android:textSize="16sp" />

                <Button
                    android:id="@+id/login_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@drawable/yellow_selector"
                    android:minHeight="0dp"
                    android:paddingBottom="7dp"
                    android:paddingTop="7dp"
                    android:text="@string/login"
                    android:textAllCaps="true"
                    android:textSize="18sp" />

            </LinearLayout>

        </LinearLayout>

    </ScrollView>
</RelativeLayout>

What's going wrong is when I uncommented the header for the screen, it doesn't scroll up but with it commented out it works as intended...what am I missing?

isuPatches
  • 6,384
  • 2
  • 22
  • 30

1 Answers1

0

Try scrolling to the view:

ScrollView scrollView = (ScrollView)findViewById(R.id.scrollview);
EditText editText = (EditText)findViewById(R.id.editText);
int[] coordinates = new int[2];
editText.getLocationOnScreen(coordinates);
scrollView.smoothScrollTo(coordinates[0],coordinates[1]);
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • I've tried that a few times...the issue I'm finding with that route is that it doesn't scroll the first time when the keyboard first pops up – isuPatches Jul 24 '15 at 15:54
  • @isuPatches - are you using full screen mode? – Kristy Welsh Jul 24 '15 at 16:03
  • http://stackoverflow.com/questions/19859474/scrollview-not-working-when-keyboard-is-showing-in-android/22067230#22067230 – Kristy Welsh Jul 24 '15 at 16:07
  • So my app theme inherits from ` – isuPatches Jul 24 '15 at 17:11
  • So I tried a few things and what did work is adding a postDelayed of 1700-2000ms and then scrolling. the issue seems to be as the keyboard is opening the scroll won't work but when it is open it's fine. I'm still trying to wrap my head around a way to dynamically wait for that to happen. – isuPatches Jul 24 '15 at 21:08