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

[Android] How to solve the annoyed 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, the toolbar stretched and occupied the whole view. It’s really annoyed, because users cannot see anything when they open this view, unless they close the keyboard.

Screenshot_20160913-223903

I thought it was an EditText 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"/>
        </LinearLayout>
</LinearLayout>

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

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

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

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

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

Screenshot_20160913-223714

(Visited 59 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.