1

Why is it that I can not get my fragments to start their layout under the app bar (it always starts behind the app bar?)

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

and my fragment is (nothing fancy, and yet if I use fragments and replay mymain, it always appears under the Toolbar vs starting just below it?)

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mymain"
        >

    </FrameLayout>
</FrameLayout>
justdan0227
  • 1,374
  • 18
  • 48
  • You need to set the `app:layout_behavior` for the sibling ViewGroup of the AppBarLayout: [this post](https://stackoverflow.com/questions/31306884/android-recyclerview-below-toolbar) shows how to do it – Bö macht Blau Nov 16 '18 at 17:52
  • Can you post whole layout? I think your main container is FrameLayout or something simillar. Try linear layout with vertical orientation maybe – Janusz Hain Nov 16 '18 at 17:54

1 Answers1

1

I use something similar:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/settings_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="0dp"
    android:paddingStart="0dp"
    android:paddingEnd="0dp" />

Then in activity onCreate :

getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new YourFragmentClass())
                .commit();
RonTLV
  • 2,376
  • 2
  • 24
  • 38