Ahmed Nawara
Ahmed Nawara

Reputation: 349

Android activity with primary and secondary drawers

I am trying to implement a UI similar to that of Google+ where the android activity would have two slide out drawers, the main one from the left and that's the main menu. The secondary one from the right and that's my notifications. However, looking at DrawerLayout I can see that it does not support. Any idea how the Google+ app implements it?

Thanks in advance

Upvotes: 1

Views: 2518

Answers (2)

Ahmed Nawara
Ahmed Nawara

Reputation: 349

I've figured out how to do it using the following layout

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
<ListView
    android:id="@+id/right_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Funny the documentation did not mention this and when the library first came out the DrawerLayout would throw an exception if more than two children existed.

Upvotes: 3

Litrik De Roy
Litrik De Roy

Reputation: 1032

The DrawerLayout does support 2 drawers in a single layout file. Both should get a different value for their layout_gravity attribute ("left" and "right").

Example snippet:

<?xml version="1.0" encoding="utf-8"?>
<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">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Main content -->

    </LinearLayout>

    <FrameLayout
        android:id="@+id/nav_left"
        android:layout_width="@dimen/nav_left_width"
        android:layout_height="match_parent"
        android:layout_gravity="left">

        <!-- Content of your left drawer -->

    </FrameLayout>

    <FrameLayout
        android:id="@+id/nav_right"
        android:layout_width="@dimen/nav_right_width"
        android:layout_height="match_parent"
        android:layout_gravity="right">

        <!-- Content of your right drawer -->

    </FrameLayout>

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

Upvotes: 0

Related Questions