Manikandan
Manikandan

Reputation: 1832

How to create a custom navigation drawer in android?

Hi iam doing an android project in which i want to show navigation drawer in all the pages. in the home screen in want to show navigation drawer in left side top and the picture of the user who logged in the app in the right top of the screen inside a circle.

enter image description here

So please help me if anybody knows to create the custom action bar with navigation drawer.

Upvotes: 1

Views: 726

Answers (1)

The Fish
The Fish

Reputation: 113

You need to add a menu file, in the menu folder, for the images to the right.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/your_id"
    android:icon="@drawable/the image you want"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="Your title"/>
</menu>

Then in the activity you want to put this code in (outside onCreate):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    return true;
}

This will put the menu in the top.

Now for the image at the right (the three lines) put this in your xml for the activity (this also adds the drawer on the side, you can customize this as you would any list view)

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

<!-- The main content goes here -->

 <!-- The navigation drawer -->

<ListView  
    android:id="@+id/drawer1"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFFFFF"
    android:choiceMode="singleChoice"
    android:divider="#FF0000"
    android:dividerHeight="1dp"
    android:paddingLeft="15sp"
    android:paddingRight="15sp" />

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

Then put this part in your java. you need to get ic-drawer image from somewhere but there all over the place so just google for it.

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout1);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    drawer, /* DrawerLayout object */
    R.drawable.ic_drawer, /* This is the image for top left*/
    R.string.drawer_open, /* "open drawer" description */
    R.string.drawer_close /* "close drawer" description */
    );

Upvotes: 1

Related Questions