Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Drawer on top ActionBar

I'm trying to make the navigation drawer over the action bar when it was slide to the right like this app: [Removed]

This is my main activity's layout:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout ...>     <RelativeLayout android:orientation="vertical"          android:layout_width="fill_parent"          android:layout_height="fill_parent">         ...     </RelativeLayout>     <fragment android:name="com...."          android:layout_gravity="start"          android:id="@id/navigation"          android:layout_width="@dimen/navigation_menu_width"          android:layout_height="fill_parent" /> </android.support.v4.widget.DrawerLayout> 

Some other questions on stackoverflow are similar such as this question but all answers are recommend to use sliding menu lib. But this app they still use android.support.v4.widget.DrawerLayout and they succeed. Don't ask me how I know they use the standard navigation drawer but I sure about it.

Would be really appreciate for your helps.


HERE IS THE FINAL SOLUTION: many thanks to @Peter Cai THIS WORKS PERFECTLY. https://github.com/lemycanh/DrawerOnTopActionBar

Screen CaptureTranslucent on KitKat

like image 923
lemycanh Avatar asked Apr 25 '14 13:04

lemycanh


People also ask

How do I open a navigation drawer?

The user can view the navigation drawer when they swipe a finger from the left edge of the activity. They can also find it from the home activity (the top level of the app) by tapping the app icon (also known as the Android "hamburger" menu) in the action bar.


1 Answers

I have a tiny "trick" learnt from https://github.com/jfeinstein10/SlidingMenu to implement the effect you required.

You only need to remove the first child of the window's decor view, and add the first child to your drawer's content view. After that, you only need to add your drawer to the window's decor view.

Below is some detailed steps for you to do that.

First, create a xml named "decor.xml" or anything you like. Only put the DrawerLayout and the drawer in. The "FrameLayout" below is just a container. We will use it to wrap your activity's content.

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout ...>     <FrameLayout android:id="@+id/container"         android:orientation="vertical"          android:layout_width="fill_parent"          android:layout_height="fill_parent"/>     <fragment android:name="com...."          android:layout_gravity="start"          android:id="@id/navigation"          android:layout_width="@dimen/navigation_menu_width"          android:layout_height="fill_parent" /> </android.support.v4.widget.DrawerLayout> 

and then remove the DrawerLayout in your main layout. Now the layout of your main activity should look like

<RelativeLayout android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent">     ... </RelativeLayout> 

we assume that the main activity's layout is named "main.xml".

in your MainActivity, write as the following:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      // Inflate the "decor.xml"     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);     DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.      // HACK: "steal" the first child of decor view     ViewGroup decor = (ViewGroup) getWindow().getDecorView();     View child = decor.getChildAt(0);     decor.removeView(child);     FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now.     container.addView(child);      // Make the drawer replace the first child     decor.addView(drawer);      // Do what you want to do.......  } 

Now you've got a DrawerLayout which can slide over the ActionBar. But you might find it covered by status bar. You might need to add a paddingTop to the Drawer in order to fix that.

like image 159
Peter Cai Avatar answered Sep 30 '22 13:09

Peter Cai