GordonW
GordonW

Reputation: 1120

Soft keyboard pushing toolbar into top status bar

I have gone through at least ten similar SO questions and nothing seems to work.

I have a simple relative layout with a toolbar, text views, edit text, etc (see xml below).

When edit text is focused I need the soft keyboard to push my layout up but under the toolbar (i.e. toolbar is pinned) but instead the keyboard pushes the entire layout up, toolbar included?

I have tried adjustPan, adjustResize, scrollviews, etc with no success, any help with this would be appreciated.

layout xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.Test">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:id="@+id/appbar_layout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    </android.support.v7.widget.Toolbar>

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

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/appbar_layout">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">



        <TextView
            android:id="@+id/tv_title"
            android:textSize="25dp"
            android:textColor="@color/colorPrimaryDark"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/iv"
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/tv_date"
            android:textSize="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/iv"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tv_title"/>



        <ImageView
            android:id="@+id/iv"
            android:layout_marginRight="-6dp"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_alignParentRight="true"
            android:maxHeight="150dp"

            />


        <TextView
            android:id="@+id/tv_description"
            android:layout_marginTop="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/iv"
            android:layout_centerHorizontal="true"
            android:textSize="15dp"
            android:textColor="@color/colorPrimaryDark"/>

        <RatingBar
            android:id="@+id/rb"
            style="?android:attr/ratingBarStyle"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_description"
            android:numStars="5" />

        <View
            android:id="@+id/view"
            android:layout_below="@+id/rb"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="1dp"
            android:visibility="visible"
            android:background="@color/colorPrimary"/>

        <EditText
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:padding="@dimen/activity_horizontal_margin"
            android:layout_below="@+id/view"
            android:gravity="top"
            android:background="@android:color/transparent"
            />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/textColorPrimary"
            android:textSize="20dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:layout_alignParentBottom="true"
            />
    </RelativeLayout>
</ScrollView>

</RelativeLayout>

Upvotes: 4

Views: 1687

Answers (2)

C&#233;sar Cobo
C&#233;sar Cobo

Reputation: 618

I use this code on the onCreateView of my fragment, it works fine.

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Upvotes: 2

user3623735
user3623735

Reputation: 343

Use CollapsingToolbarLayout

<android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
    <android.support.v7.widget.Toolbar
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:layout_collapseMode="pin"/>
    </android.support.design.widget.CollapsingToolbarLayout>

and make sure you add app:layout_collapseMode="pin" flag to your Toolbar in your layout.xml.

Upvotes: 0

Related Questions