Z. Charles Dziura
Z. Charles Dziura

Reputation: 933

Adding Fragments to an Activity That's Using a DrawerLayout

I'm building an Android app which utilizes the Navigation Drawer UI pattern for top-level navigation. In order to facilitate this, I created a custom Activity (called DrawerActivity) which automatically initializes the DrawerLayout. For each DrawerActivity's "content" view, I want to use Fragments.

By default, every DrawerActivity will inflate the following layout:

<android.support.v4.widget.DrawerLayout 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"
    android:id="@+id/drawer_drawerLayout"
    tools:context=".activities.DrawerActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ListView
        android:id="@+id/drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="@color/drawer_background" />

</android.support.v4.widget.DrawerLayout>

I'm also going to support various sized tablets with my app, so I require the capability to dynamically load Fragments into the main "content" view of the layout. Not a problem, I already know how to initialize a FragmentTransaction to take care of that for me.

However, the DrawerLayout only seems to work properly when I have one "content" widget, commonly placeheld by a FrameLayout. If I want to replace the FrameLayout with a Fragment, I'd simply build a FragmentTransaction and go from there. However, I'd like to automatically inflate two or more Fragments if necessary (depending on if the user is using a tablet or not).

My Question: What would be the "best" way to build my UI using Fragments, all while supporting the Navigation Drawer UI pattern? Should I instead use an <include /> tag for the DrawerActivity's "content" view (which would automatically choose the correct layout required based on orientation and screen size), or should I load in the Fragments myself?

Upvotes: 0

Views: 1438

Answers (1)

Larry McKenzie
Larry McKenzie

Reputation: 3283

This can be done by building building a separate layout with the same name and placing it in the 'res/layout-sw600dp' directory:

<android.support.v4.widget.DrawerLayout 
    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"
    android:id="@+id/drawer_drawerLayout"
    tools:context=".activities.DrawerActivity">

    <LinearLayout
        android:id="@+id/content_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientaion="horizontal" >

            <FrameLayout
                android:id="@+id/content_start"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:weight="1" />

            <FrameLayout
                android:id="@+id/content_end"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:weight="1" />

    </LinearLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="@color/drawer_background" />

</android.support.v4.widget.DrawerLayout>

Then you can programmatically check for the content_container id and load the appropriate fragments based on if it exists or not.

Upvotes: 1

Related Questions