27

I am trying to develop an activity with a toolbar (the example is more or less taken from a tutorial), but the Toolbar always overlaps part of the other content. Here is a screenshot:

enter image description here

The blue toolbar overlaps some of the other content. I have tried to search for a similar question on SO but only found something unrelated. I also tried to change the order of some elements and replaced wrap_content <-> match_parent which at most worsens the layout.

I am sure I am missing something very fundamental, but I do not see what.

Code of activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

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

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

Code of content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
  <GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="wrap_content"
        android:layout_height="match_parent"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

    <TextView
            android:text="@string/MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            />

    <TextView
            android:text="You can configure email in just a few steps:"
            android:textSize="16dip"

            android:layout_columnSpan="4"
            android:layout_gravity="left"
            />

    <TextView
            android:text="Email address:"

            android:layout_gravity="right"
            />

    <EditText
            android:ems="10"
            />

    <TextView
            android:text="Password:"

            android:layout_column="0"
            android:layout_gravity="right"
            />

    <EditText
            android:ems="8"
            />

    <Space
            android:layout_row="4"
            android:layout_column="0"
            android:layout_columnSpan="3"
            android:layout_gravity="fill"
            />

    <Button
            android:text="Next"
            android:id="@+id/imageButton1"
            android:layout_row="5"
            android:layout_column="3"
            />
</GridLayout>
Henry
  • 17,490
  • 7
  • 63
  • 98
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Look at my answer. You still can use the `` tag. Don't replace the whole thing in the main layout. – Henry Oct 29 '15 at 19:14

6 Answers6

87

Replace your <include layout="@layout/content_main"/> with this:

   <include layout="@layout/content_main" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
x4h1d
  • 6,042
  • 1
  • 31
  • 46
Henry
  • 17,490
  • 7
  • 63
  • 98
  • This worked for me. It also works to add the layout_behavior thing in the "content_main", I mean, in the included file instead of the activity_main – Jahaziel Jul 06 '16 at 19:35
  • Worked perfectly. I first forgot to include `compile 'com.android.support:design:26.0.1'` so app:layout_behaviour was not found. – Yuliwee Jan 20 '18 at 16:12
25

Add this to the view below the toolbar

app:layout_behavior="@string/appbar_scrolling_view_behavior"
s-hunter
  • 24,172
  • 16
  • 88
  • 130
8

try this in your layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

    <GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            />

        <TextView
            android:text="You can configure email in just a few steps:"
            android:textSize="16dip"

            android:layout_columnSpan="4"
            android:layout_gravity="left"
            />

        <TextView
            android:text="Email address:"

            android:layout_gravity="right"
            />

        <EditText
            android:ems="10"
            />

        <TextView
            android:text="Password:"

            android:layout_column="0"
            android:layout_gravity="right"
            />

        <EditText
            android:ems="8"
            />

        <Space
            android:layout_row="4"
            android:layout_column="0"
            android:layout_columnSpan="3"
            android:layout_gravity="fill"
            />

        <Button
            android:text="Next"
            android:id="@+id/imageButton1"
            android:layout_row="5"
            android:layout_column="3"
            />
    </GridLayout>

</android.support.design.widget.CoordinatorLayout>
Aakash
  • 5,181
  • 5
  • 20
  • 37
6

Just a simple change did the trick.

My Previous code:

activity_device_scan.xml

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

New

activity_device_scan.xml:

    <include layout="@layout/content_device_scan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
0

if

"@string/appbar_scrolling_view_behavior"

doesn't work, then try to call it directly, like

app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
A P
  • 2,131
  • 2
  • 24
  • 36
Daniel
  • 1
  • 1
0

I changed the CoordinatorLayout for activity_main to a vertical linear layout and it worked. I had Android Studio convert the view type for me and at first it broke my inclusion of the content_main. I restored the include callout as part of my conversion.

MustardMan
  • 43
  • 1
  • 7