Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Drawer and windowActionBarOverlay = true

Tags:

I'm trying to implement the new Android Navigation Drawer in my application. I have created a BaseActivity.java that handles the Drawer setup and listeners, and I have two subactivities that extend this base class. On the second activity, I plan to use a different action bar style, using the following attrs:

<item name="android:windowActionBarOverlay">true</item> <item name="android:background">@android:color/transparent</item> 

to make the action bar transparent, and make content richer, as there is a picture header in my layout.

I've achieved just that, but now the problem is, that because the content is expanding to take advantage of the extra space of using the ActionBar as overlay, the Navigation Drawer itself is expanding too and it overlaps the ActionBar, creating a pretty awful looking layout:

enter image description here

What I'd like to have done, is the actual content (frame layout that will be populated with a fragment) to take up the extra space, but have the nav drawer still go underneath the action bar, similar to the Play Music App:

enter image description here

Any ideas on what I can do to make that happen?

EDIT So, as per Ahmad's assistance I set the marginTop on the ListView only. Here's the layout:

<!-- The navigation drawer --> <ListView android:id="@+id/left_drawer"           android:layout_marginTop="?android:attr/actionBarSize" <!-- This was added after seeing the crazy effect, but does nothing -->           android:layout_marginBottom="0dp"           android:layout_marginLeft="0dp"           android:layout_marginRight="0dp"           android:layout_width="240dp"           android:layout_height="fill_parent"           android:layout_gravity="start"           android:choiceMode="singleChoice"           android:background="?attr/listviewBackground"           /> 

And now, it works great for the top side, but for some reason there's also a margin at the bottom of the view, which doesn't make any sense to me at all. Here's a screenshot.

Not sure what's causing it :(

like image 692
daniel_c05 Avatar asked May 30 '13 23:05

daniel_c05


People also ask

What is the use of navigation drawer in Android?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.

What is the purpose of DrawerLayout and NavigationView?

Drawer Layout is the root layout in which we define a FrameLayout and a Navigation View. In Navigation View we set the items from menu file and FrameLayout is used to replace the Fragments on the click of menu items.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .


2 Answers

And now, it works great for the top side, but for some reason there's also a margin at the bottom of the view, which doesn't make any sense to me at all. Here's a screenshot.

If you set your ListView gravity to start|bottom it solves your problem. No additional margin is added at the bottom. Looks like the DrawerLayout default gravity is start|center

<ListView     android:id="@+id/left_drawer"     android:layout_marginTop="?android:attr/actionBarSize"     android:layout_width="240dp"     android:layout_height="match_parent"     android:layout_gravity="start|bottom"/> 
like image 189
kofi Avatar answered Oct 11 '22 23:10

kofi


In case anyone is interested in another take to this question. Here's what happened.

I tried setting only the margin to the top of the list view like this:

android:layout_marginTop="?android:attr/actionBarSize" 

But as mentioned on the edited question, that had a weird behaviour where there was also a margin on the bottom despite not being set on the layout resource file.

So, I was looking closely at the Play Music App and noticed that it's not actually a margin, but rather some padding, and additionally they are using a custom background that fills the space specified by the padding with a transparent color.

Here's what I did:

  • Set Padding at the top of the ListView, rather than margin:

    android:paddingTop="?android:attr/actionBarSize"

As said before, it's important to not hard code the dimensions as they vary per device.

  • Create a custom drawable that has a top part transparent, and then rest of a solid color:

It looks somehow like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item>     <shape  android:shape="rectangle">         <solid android:color="#80000000" />     </shape> </item>  <item android:top="@dimen/action_bar_default_height">     <shape             android:shape="rectangle">         <solid android:color="@color/light_gray" />     </shape> </item> 

Note that I tried to use ?android:attr/actionBarSize on the drawable, but that made the app force close. Instead, I searched through grepcode and found a few dimen files with different sizes for the action bar, so I added those to my own project's dimen files.

  • For values: 48dp
  • For values-land: 40dp
  • For values-sw600dp: 56dp

And after that, I think I looks great, notice on the screenshot how the listview and the actionbar don't overlap, and the transparent part of the listview is just the right size.

enter image description here

Hope that helps anyone who was wondering how to achieve this.

like image 43
daniel_c05 Avatar answered Oct 12 '22 01:10

daniel_c05