user6265109 user6265109 - 6 months ago 17
Android Question

How to make the layout scrollable when the keyboard is active?

I have a register layout. In this if I click on first edit text the keyboard appears and other views gets hidden under the keyboard. So I want to make the layout scrollable so we can scroll up and use the next edit text while the keyboard is popped up.

I tried to add the scroll view for this in layout. Also set this android:windowSoftInputMode="adjustResize" in manifest for the activity. But it makes all the views shift up , so all the layouts got merged with each other.

Following is the image what I got.

enter image description here

EDIT: Edited layout : Edited again.Still it works same.



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<RelativeLayout android:id="@+id/login" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/imageView2"
android:src="@drawable/logo_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:id="@+id/linearLayout5">

<LinearLayout android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:id="@+id/linearLayoutSecondName"
android:background="@drawable/field_1"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true">

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView3"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:background="@drawable/user" />

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="40dp" android:hint="@string/user_id"
android:maxLines="1"
android:singleLine="true"
android:inputType="text"
android:layout_below="@+id/linearLayoutFirstName"
android:layout_toRightOf="@+id/linearLayoutFirstName"
android:layout_toEndOf="@+id/linearLayoutFirstName"
android:background="@android:color/transparent"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:textSize="12sp" />
</LinearLayout>

<LinearLayout android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:id="@+id/linearLayoutFirstName"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/linearLayoutSecondName"
android:layout_below="@+id/linearLayoutSecondName"
android:layout_marginTop="20dp"
android:background="@drawable/field_1">

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView4"
android:layout_marginLeft="15dp"
android:background="@drawable/email"
android:layout_gravity="center" />

<EditText android:id="@+id/password" android:layout_width="match_parent"
android:layout_height="40dp" android:hint="EMAIL "
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:maxLines="1" android:singleLine="true"
android:layout_gravity="center"
android:layout_below="@+id/linearLayoutSecondName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:background="@android:color/transparent"
android:textSize="12sp"
android:inputType="textEmailAddress" />
</LinearLayout>

<LinearLayout
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:id="@+id/linearLayout4"
android:background="@drawable/field_1"
android:layout_below="@+id/linearLayout5"
android:layout_alignRight="@+id/imageView2"
android:layout_alignEnd="@+id/imageView2"
android:layout_marginTop="20dp">

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView5"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:background="@drawable/pass" />

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="@string/prompt_password"
android:maxLines="1"
android:singleLine="true"
android:inputType="textPassword|text"
android:layout_below="@+id/linearLayoutFirstName"
android:layout_toRightOf="@+id/linearLayoutFirstName"
android:layout_toEndOf="@+id/linearLayoutFirstName"
android:background="@android:color/transparent"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:textSize="12sp" />
</LinearLayout>

<LinearLayout
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:id="@+id/linearLayout6"
android:background="@drawable/field_1"
android:layout_below="@+id/linearLayout5"
android:layout_alignLeft="@+id/linearLayout5"
android:layout_alignStart="@+id/linearLayout5"
android:layout_marginTop="20dp">

<ImageView
android:layout_width="15dp"
android:layout_height="25dp"
android:id="@+id/imageView6"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:background="@drawable/phone" />

<EditText
android:layout_width="30dp"
android:layout_height="match_parent"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_marginLeft="20dp"
android:background="@android:color/transparent"
android:hint="+91"
android:textSize="14sp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background = "@android:color/darker_gray"
android:layout_marginTop="05dp"
android:layout_marginBottom="05dp">
</View>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="MOBILE NO"
android:maxLines="1"
android:singleLine="true"
android:layout_below="@+id/linearLayoutFirstName"
android:layout_toRightOf="@+id/linearLayoutFirstName"
android:layout_toEndOf="@+id/linearLayoutFirstName"
android:background="@android:color/transparent"
android:layout_gravity="center"
android:textSize="12sp"
android:layout_marginLeft="05dp"
android:inputType="number" />
</LinearLayout>

</LinearLayout>


<Button android:id="@+id/sign_up_button"
android:layout_width="match_parent" android:layout_height="40dp"
android:text="SIGN UP"
android:textSize="14sp"
android:textColor="@android:color/white"
style="?android:attr/borderlessButtonStyle"
android:layout_gravity="left"
android:background="@drawable/rounded_shape"
android:layout_alignParentBottom="false"
android:layout_alignRight="@+id/linearLayout5"
android:layout_alignEnd="@+id/linearLayout5"
android:layout_alignLeft="@+id/linearLayout5"
android:layout_alignStart="@+id/linearLayout5"
android:layout_below="@+id/linearLayout5"
android:layout_marginTop="40dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="@string/register"
android:id="@+id/alreadyRegistered"
android:focusable="false"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textColorLink="@android:color/darker_gray"
android:textIsSelectable="true"
android:textSize="14sp"
android:layout_marginBottom="10dp"
android:textAlignment="center" />

</RelativeLayout>
</ScrollView>




How to do this?

I an trying same with another layout, but I can't scroll up while the keyboard is active. Please help.

login layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:id="@+id/parentPanel">

<ScrollView
android:id="@+id/my_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/imageView2"
android:src="@drawable/logo_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout5"
android:layout_below="@+id/imageView2"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">

<LinearLayout android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:id="@+id/linearLayoutSecondName"
android:background="@drawable/field_1"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp">

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView3"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:background="@drawable/user" />

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="40dp" android:hint="@string/user_id"
android:maxLines="1"
android:singleLine="true"
android:inputType="text"
android:layout_below="@+id/linearLayoutFirstName"
android:layout_toRightOf="@+id/linearLayoutFirstName"
android:layout_toEndOf="@+id/linearLayoutFirstName"
android:background="@android:color/transparent"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:textSize="12sp" />
</LinearLayout>

<LinearLayout android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:id="@+id/linearLayoutFirstName"
android:background="@drawable/field_1"
android:layout_below="@+id/linearLayoutSecondName"
android:layout_alignLeft="@+id/linearLayoutSecondName"
android:layout_alignStart="@+id/linearLayoutSecondName"
android:layout_marginTop="20dp">

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView4"
android:layout_marginLeft="15dp"
android:background="@drawable/pass"
android:layout_gravity="center" />

<EditText android:id="@+id/password" android:layout_width="match_parent"
android:layout_height="40dp" android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified" android:inputType="textPassword"
android:maxLines="1" android:singleLine="true"
android:layout_gravity="center"
android:layout_below="@+id/linearLayoutSecondName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:background="@android:color/transparent"
android:textSize="12sp" />
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_below="@+id/linearLayoutFirstName"
android:layout_alignLeft="@+id/linearLayoutFirstName"
android:layout_alignStart="@+id/linearLayoutFirstName"
android:layout_alignRight="@+id/linearLayoutFirstName"
android:layout_alignEnd="@+id/linearLayoutFirstName"
android:layout_marginTop="40dp">
<Button android:id="@+id/sign_up_button"
android:textSize="14sp"
android:layout_width="110dp" android:layout_height="40dp"
android:text="SIGN UP"
android:textColor="@color/colorPrimary"
android:elevation="0dp"
android:background="@drawable/signup"
style="?android:attr/borderlessButtonStyle"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/linearLayoutFirstName"
android:layout_alignStart="@+id/linearLayoutFirstName"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />

<Button
android:id="@+id/login"
android:textSize="14sp"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="LOGIN"
android:elevation="0dp"
android:layout_gravity="right"
android:background="@drawable/rounded_shape"
android:textColor="#ffffff"
style="?android:attr/borderlessButtonStyle"
android:layout_alignTop="@+id/sign_up_button"
android:layout_alignRight="@+id/linearLayoutFirstName"
android:layout_alignEnd="@+id/linearLayoutFirstName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="@string/forgotPass"
android:id="@+id/forgotPass"
android:textColorLink="@android:color/darker_gray"
android:textIsSelectable="true"
android:textSize="14sp"
android:focusable="false"
android:textAlignment="center"
android:layout_gravity="bottom|center_horizontal"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:layout_above="@+id/textView2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
</RelativeLayout>
</ScrollView>

</RelativeLayout>

Answer

UseScroll View as Your Root Layout.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

Also add this to your Manifest.

<activity android:name="YourActivity" 
          android:windowSoftInputMode="adjustResize" />