TatiOverflow
TatiOverflow

Reputation: 1171

Adding fragment below toolbar

I have an activity.xml.

As you can see, it's a relative layout. I am hoping that the Frame layout which will contain the fragment, will be below the toolbar.

The fragment is added, for sure. But it's added at the top, covering the toolbar. Just wondering what I am doing wrong.

The website wouldn't let me add code for some reason. It was telling me it's not properly formatted. here is the activity.xml layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    >

    <!--scroll|snap-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarHeight"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appToolBar"/>

</RelativeLayout>

And this is simply what I am doing in the Activity.java file.

getFragmentManager().beginTransaction().add(android.R.id.content, SetupFrag.createInstance(false), SETUP_FRAGMENT).commit();

Upvotes: 1

Views: 5178

Answers (2)

Simon Marquis
Simon Marquis

Reputation: 7526

You should use a different root Layout.

RelativeLayout will layout its children relatively to each other. What you want instead is a LinearLayout with a vertical orientation.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarHeight"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Upvotes: 1

ataulm
ataulm

Reputation: 15334

You're using an existing id, @android:id/content

You should use + to add your own ID, @+id/content, otherwise the fragment is being added to the content view (the level above your activity layout).

Upvotes: 3

Related Questions