How to make Toolbar not overlap other content in Android?
Asked Answered
P

6

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>
Perry answered 29/10, 2015 at 18:42 Comment(1)
Look at my answer. You still can use the <include> tag. Don't replace the whole thing in the main layout.Inalterable
E
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>
Ereshkigal answered 29/10, 2015 at 18:49 Comment(8)
I suppose I put this into the activity_main.xml?Perry
When I do so, the preview is completly empty!Perry
yes you are supposed to for sure, if using android studio, refresh the preview onceEreshkigal
Wow seems to work finally! But what does app:layout_behavior exactly do?Perry
it adjusts according to co-ordnitaor layout, it is used in context to co-ordinator layoutEreshkigal
Let us continue this discussion in chat.Perry
@Ereshkigal You need to use the layout_behaviour. But you need to do this in the <include> tag.Inalterable
i knew that, just wanted to clarify to alexEreshkigal
I
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"/>
Inalterable answered 29/10, 2015 at 19:12 Comment(2)
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_mainClinkstone
Worked perfectly. I first forgot to include compile 'com.android.support:design:26.0.1' so app:layout_behaviour was not found.Fraenum
S
25

Add this to the view below the toolbar

app:layout_behavior="@string/appbar_scrolling_view_behavior"
Smoker answered 2/12, 2016 at 23:34 Comment(1)
i had to add this to the content layout file. thanksLishalishe
E
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>
Ereshkigal answered 29/10, 2015 at 18:49 Comment(8)
I suppose I put this into the activity_main.xml?Perry
When I do so, the preview is completly empty!Perry
yes you are supposed to for sure, if using android studio, refresh the preview onceEreshkigal
Wow seems to work finally! But what does app:layout_behavior exactly do?Perry
it adjusts according to co-ordnitaor layout, it is used in context to co-ordinator layoutEreshkigal
Let us continue this discussion in chat.Perry
@Ereshkigal You need to use the layout_behaviour. But you need to do this in the <include> tag.Inalterable
i knew that, just wanted to clarify to alexEreshkigal
P
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"/>
Phosphorous answered 20/7, 2017 at 12:34 Comment(0)
U
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"
Universalize answered 16/5, 2019 at 12:35 Comment(0)
O
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.

Omphale answered 26/12, 2019 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.