user5448913
user5448913

Reputation:

Adjust RelativeLayout above keyboard

I have the following .xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_100"
android:orientation="vertical"
android:weightSum="4">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/login_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/welcome_bottom_margin"
        android:layout_marginTop="@dimen/login_logo_margin_top"
        android:theme="@style/ThemeOverlay.MyTitleText"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/status"
        style="@style/ThemeOverlay.MyTextDetail"
        android:text="@string/signed_out" />

</LinearLayout>


<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/grey_300"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/email_password_fields"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingRight="16dp">

        <EditText
            android:id="@+id/field_email"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/hint_email"

            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/field_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/hint_password"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/email_password_buttons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email_password_fields"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">

        <Button
            android:id="@+id/email_sign_in_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/sign_in"
            android:theme="@style/ThemeOverlay.MyDarkButton" />

        <Button
            android:id="@+id/email_create_account_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/create_account"
            android:theme="@style/ThemeOverlay.MyDarkButton" />

    </LinearLayout>      

</RelativeLayout>

</LinearLayout>

That looks like this. But when I open my keyboard it hides 80% of the RelativeLayout.

How may I move the layout above the keyboard?

I have found a diversity of answers but none of them seem to work.

EDIT:

Even after adding android:windowSoftInputMode="adjustResize" in my Manifest like such:

<activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
 </activity>`

I get the following outcome:

outcome.

Upvotes: 0

Views: 2031

Answers (4)

Jagruttam Panchal
Jagruttam Panchal

Reputation: 3200

Set softInputMode for your activity in AndroidManifest.xml

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

And replace xml with below code:

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

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/login_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/welcome_bottom_margin"
                android:layout_marginTop="@dimen/login_logo_margin_top"
                android:theme="@style/ThemeOverlay.MyTitleText"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/status"
                style="@style/ThemeOverlay.MyTextDetail"
                android:text="@string/signed_out" />

        </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:background="@color/grey_300"
            android:gravity="center_vertical">

            <LinearLayout
                android:id="@+id/email_password_fields"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="16dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:paddingRight="16dp">

                <EditText
                    android:id="@+id/field_email"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="@string/hint_email"

                    android:inputType="textEmailAddress" />

                <EditText
                    android:id="@+id/field_password"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="@string/hint_password"
                    android:inputType="textPassword" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/email_password_buttons"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/email_password_fields"
                android:orientation="horizontal"
                android:paddingLeft="16dp"
                android:paddingRight="16dp">

                <Button
                    android:id="@+id/email_sign_in_button"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/sign_in"
                    android:theme="@style/ThemeOverlay.MyDarkButton" />

                <Button
                    android:id="@+id/email_create_account_button"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/create_account"
                    android:theme="@style/ThemeOverlay.MyDarkButton" />

            </LinearLayout>

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

Upvotes: 2

Horrorgoogle
Horrorgoogle

Reputation: 7868

Simply Add your this line of code in your activity:

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

Maybe help for you, have your try these line of code. More info: How to adjust layout when soft keyboard appears

Upvotes: 1

Robin Bruneel
Robin Bruneel

Reputation: 1101

Use <activity android:windowSoftInputMode="adjustResize"> </activity> or <activity android:windowSoftInputMode="adjustPan"> </activity>, you can see the difference here Difference between adjustResize and adjustPan in android?

Upvotes: 0

Akshay Panchal
Akshay Panchal

Reputation: 695

Add

android:windowSoftInputMode="adjustResize" 

in your manifest in Activity tag

Upvotes: 1

Related Questions