VinayakPurohit
VinayakPurohit

Reputation: 45

How to scroll layout when keyboard pops up in android

I have made the following layout using coordinator layout. But when the keyboard pops up some of the elements go below the keyboard hence losing the visibility. So please help me to fix that.I have tried putting adjustPan, adjustResize in windowSoftInputMode but still not working. Even tried with the nested scroll view, no success.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@drawable/gradient"
            android:gravity="center">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center|center_vertical"
                android:background="@drawable/app_main_logo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|center_vertical"
                android:layout_marginTop="20dp"
                android:text="My App"
                android:textColor="#fff"
                android:textSize="20dp" />
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/loginCardView"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="250dp"
            app:cardCornerRadius="10dp"
            app:cardElevation="8dp">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingLeft="30dp"
                android:paddingRight="30dp">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="#fff">

                    <EditText
                        android:id="@+id/input_email_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:layout_marginTop="40dp"
                        android:hint="Username"
                        android:inputType="textEmailAddress"
                        android:textColor="#fff"
                        android:textColorHint="#fff" />
                </android.support.design.widget.TextInputLayout>

                <!-- Password Label -->
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="#fff"
                    app:passwordToggleEnabled="true">

                    <EditText
                        android:id="@+id/input_password_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Password"
                        android:inputType="textPassword"
                        android:textColor="#fff"
                        android:textColorHint="#fff" />
                </android.support.design.widget.TextInputLayout>

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/capsule_button"
            android:elevation="20dp"
            android:gravity="center"
            android:text="Sign In"
            android:textColor="#fff"
            android:textSize="20dp"
            app:layout_anchor="@id/loginCardView"
            app:layout_anchorGravity="bottom|center" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Don't have an account?" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:text="Create Now"
                android:textStyle="bold" />
        </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

Upvotes: 1

Views: 363

Answers (2)

Vaibhav pandey
Vaibhav pandey

Reputation: 109

Use card view to top root then use any other layout with in it...!

Upvotes: 1

deep bhatt
deep bhatt

Reputation: 27

Well you can use Scrollview,It will enable scrolling. So you can scroll all layout upside and downside.Do try this, if you don't want this you can also check Collapsing Toolbars. From here https://material.io/develop/android/components/collapsing-toolbar-layout/

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">

     //This is Scrollview you can adjust height and width as you want.

    <ScrollView android:layout_marginTop="30dp"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:id="@+id/scrollView">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@drawable/gradient"
        android:gravity="center">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center|center_vertical"
            android:background="@drawable/app_main_logo" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_vertical"
            android:layout_marginTop="20dp"
            android:text="My App"
            android:textColor="#fff"
            android:textSize="20dp" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.CardView
        android:id="@+id/loginCardView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="250dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="8dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingLeft="30dp"
            android:paddingRight="30dp">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:background="#fff">

                <EditText
                    android:id="@+id/input_email_login"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="40dp"
                    android:hint="Username"
                    android:inputType="textEmailAddress"
                    android:textColor="#fff"
                    android:textColorHint="#fff" />
            </android.support.design.widget.TextInputLayout>

            <!-- Password Label -->
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:background="#fff"
                app:passwordToggleEnabled="true">

                <EditText
                    android:id="@+id/input_password_login"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:textColor="#fff"
                    android:textColorHint="#fff" />
            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/capsule_button"
        android:elevation="20dp"
        android:gravity="center"
        android:text="Sign In"
        android:textColor="#fff"
        android:textSize="20dp"
        app:layout_anchor="@id/loginCardView"
        app:layout_anchorGravity="bottom|center" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Don't have an account?" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:text="Create Now"
            android:textStyle="bold" />
    </LinearLayout>
    </ScrollView>
    </android.support.design.widget.CoordinatorLayout>

Upvotes: 0

Related Questions