[Android] How to solve the annoying toolbar stretching issue?

I’ve been developing an Android application recently, and I use Android v7 toolbar in my app instead of ActionBar. When I opened some views with EditText component, the toolbar stretched and occupied the whole view. It’s really annoying because users cannot see anything when they open this view unless they close the keyboard.

I thought it was an EditText component issue, so I tried to adjust the EditText parameters. However, no matter what I did, it didn’t work. Finally, I found the root cause and understood how to solve it.

The root cause was:

<item name="android:windowTranslucentStatus">true</item> 

If you have this property in your style.xml, you can add android:fitsSystemWindows="true" into your root node of layout file just like below example, the toolbar stretching issue will be fixed.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical">

    <include layout="@layout/toolbar" />

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

            <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content"/>

The drawback is the colour of status bar may be changed. You can see the status bar is grey instead of light green.

Alternatively, we can add android:windowSoftInputMode="adjustPan" into AndroidManifest.xml like the below example:

        <activity android:name=".ManagerTourListActivity" android:theme="@style/AppTheme">
        <activity android:name=".ManagerAddTourActivity" android:windowSoftInputMode="adjustPan" android:theme="@style/AppTheme">

You don’t have to add this property to every activity. Only add this property to some activities having toolbar stretching issue.

After doing this, I believe your toolbar will not stretch again.

(Visited 74 times, 1 visits today)