10

I'm adding a Navigation Drawer to this app that I am developing and I have scoured the internet; forums, stackoverflow, android developer documentation, and still have not found a great answer for this.

I know that it is possible to do this without using either of these things. What I am wondering is how. The NsMenuAdapter model uses a title, and then there are these functions

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

Which are clearly looking for an action bar. I tried a couple of models that didn't work, the big one I just got done trying is located here How to Add icons adjacent to titles for Android Navigation Drawer (which is related to the link I have below, the project is from gitHub here https://github.com/gabrielemariotti/androiddev/tree/master/NavigationDrawer). Now the key thing is, I am using a custom layout (i.e. Relative Layouts mixed in with Linear Layouts) and I'm really lost on what my next step should be in order to get this to work.

Sidenote: When I only have the ListView in my main_activity.xml (the implementation for the Navigation Drawer) it does properly slide out like it is suppose to. But I cannot for the life of me figure out how to populate it with data. I basically need 3 headers with that will have clickable navigation elements in them, with icons next to the elements.

I turned to this model for most of my insight on how to do this via Relative Layouts http://gmariotti.blogspot.com/2013/05/creating-navigation-drawer.html But they use action/title bars which is what is really throwing me for a loop.

Community
  • 1
  • 1
ThorinOakenshield
  • 161
  • 1
  • 2
  • 9
  • I thinks this might helps you please check this http://stackoverflow.com/questions/20823623/how-to-implement-navigation-drawer-without-action-bar-but-opens-the-navigation-s – Kumar Subramanian Jan 02 '14 at 05:13

3 Answers3

14

It's quite simple actually. Easier than with ActionBar. I'm writing the answer with almost simplest of layouts

make your xml something like this:

<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" >

<!-- This is how your main page will look, just 2 buttons -->

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:onClick="onLeft"
            android:text="left" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="onRight"
            android:text="right" />
    </RelativeLayout>

<!-- Left Drawer -->
    <RelativeLayout
    android:id="@+id/whatYouWantInLeftDrawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_dark" />
    <!-- you can have many more widgets here like buttons or labels -->
    </RelativeLayout>


<!-- Right Drawer -->
    <RelativeLayout
    android:id="@+id/whatYouWantInRightDrawer"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_light" />
    <!-- you can have many more widgets here like buttons or labels -->
    </RelativeLayout>

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

Then make your activity something like this:

public class MainActivity extends Activity {

RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      I'm removing the ActionBar.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
        rightRL = (RelativeLayout)findViewById(R.id.whatYouWantInRightDrawer);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    }

    public  void onLeft(View view) {
         drawerLayout.openDrawer(leftRL);
    }

    public  void onRight(View view) {
         drawerLayout.openDrawer(rightRL);
    }
}

That's it. Hope it helps.

Pranav Mahajan
  • 2,048
  • 2
  • 23
  • 36
5

I know that it is possible to do this without using either of these things. What I am wondering is how.

Step #1: Follow the instructions for using DrawerLayout, such as the steps in this training guide, skipping anything related to the action bar.

Step #2: There is no step #2.

While DrawerLayout can work with the action bar, it is not required, and actually requires additional setup.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, thank you. I have read through these references multiple times, this was my last stop, hence the question. Though I do appreciate your answer, the "figure it out yourself" approach was not why I came here. I needed help and insight to whether anyone knows how to handle this, without action bars/title bars and not using FragmentLayouts. But I kindly appreciate your answer anyways. – ThorinOakenshield Jul 18 '13 at 23:51
  • @ThorinOakenshield: "I have read through these references multiple times" -- the training guide, in particular, shows you how to use a `DrawerLayout` without an action bar, just by following the first three steps. If following three steps in a tutorial is "figure it out yourself", then there is nothing that anyone is going to be able to do in a StackOverflow answer that is any better. – CommonsWare Jul 19 '13 at 00:22
  • @ThorinOakenshield: "and not using FragmentLayouts" -- there is nothing in Android called a FragmentLayout. And if you mean "fragments", please note that nothing in your question refers to fragments. – CommonsWare Jul 19 '13 at 00:23
  • You are correct, I did not mention fragments, I mentioned a "cutom layout" using Relative layouts. I should have been more specific when I stated that, but I figured I had gotten my point across. – ThorinOakenshield Jul 19 '13 at 00:25
  • @ThorinOakenshield: And note that the only place the training guide uses fragments is in handling the clicks in the `ListView` in the `DrawerLayout` navigation pane. You can delete the fragment manipulation and modify your UI in some other way. – CommonsWare Jul 19 '13 at 00:28
  • @CommonsWare : Hi Mark. I know this is a weird way of approaching anybody but I'm stuck at an issue & I don't know any person who knows android as well as you do. So can you please look at this: http://stackoverflow.com/questions/33921165/view-on-top-of-map-disappears-while-using-it-with-a-drawerlayout . I'm in desperate need of help. – Pranav Mahajan Nov 26 '15 at 07:33
0

I was trying to add navigation drawer to an already existing activity (Which was without action bar) solution for me was to remove the line:

android:fitsSystemWindows="true" 

from <android.support.v4.widget.DrawerLayout in my Activity's xml file.

Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98