How to make the drawer layout be below the actionbar/toolbar? I'm using v7:21 app compat library with the new ToolBar view.
Examples that I see looks like
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- drawer view -->
<LinearLayout
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start">
<!-- drawer content -->
</LinearLayout>
<!-- normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The toolbar -->
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- The rest of content view -->
</LinearLayout>
But then the toolbar will be hidden by the drawer, which makes an animated hamburger icon (like v7.ActionBarDrawerToggle) useless since it will not be visible below the drawer, but I do want to use the new ToolBar view to support Material theme better.
So how to accomplish that? Is it possible to have DrawerLayout as a non top-level view?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The toolbar -->
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- drawer view -->
<LinearLayout
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start">
<!-- drawer content -->
</LinearLayout>
<!-- normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The rest of content view -->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
i don't think you can when using custom toolbar
but a work around would be to set a top_margin to drawer.
(the same thing on google play store!)
<!-- drawer view -->
<LinearLayout
android:layout_marginTop="?attr/actionBarSize"
...
if you found a better solution tell me too ;)
Change Your drawer layout style like as below
RelativeLayout
----Toolbar
----DrawerLayout
---ContentView
---DrawerList
My solution: generate template with Android Studio 2.1.2, drawer template:
Only need three changes: set margin top in view NavigationView and delete overloap statusbar in style.xml
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
android:fitsSystemWindows="false"
layout main.xml set margin top get size actionbar value
android:layout_marginTop="?attr/actionBarSize"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="?attr/actionBarSize"
android:fitsSystemWindows="false"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
I had issues getting this to work, and it finally displayed properly. Here is the layout I used:
<LinearLayout...[add your namespaces, etc]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar [your custom toolbar] />
<android.support.v4.widget.DrawerLayout [your drawer layout] />
<LinearLayout>
[content here]
</LinearLayout>
<android.support.design.widget.NavigationView />
</LinearLayout>
Be careful moving the design widgets around, if one is under the wrong root tag, you'll get a
"no layout_gravity_left"
exception.
I have found a simpler solution:
Set the DrawerLayout and NavigationView attribute:
android:fitsSystemWindows="false"
and then give marginTop to navigation view as
"?actionBarSize"
maybe your statusbar background will become transparent in that case in styles.xml add
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
attribute to get back the normal color or any other color you want...
Here is the Kotlin solution:
In the layout file containing your DrawerLayout...
Add android:keepScreenOn="true" to the DrawerLayout
Add android:layout_marginTop="?android:attr/actionBarSize" to the NavigationView
The full XML piece should look like this:
<com.mullr.neurd.Miscellaneous.CustomDrawerLayout
android:id="#+id/clipped_drawer_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keepScreenOn="true"
tools:openDrawer="start"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="?android:attr/actionBarSize"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</com.mullr.neurd.Miscellaneous.CustomDrawerLayout>
That should do it (ignore my clipped nav drawer).
Related
I'm new to the drawer layout and the use of fragments.
I used the tutorial on creating an activity with a DrawerLayout.
It gave me a nullpointer on getActionBar(), so I added a toolbar to the activity main and used getSupportActionBar() instead.
Now it looks like the toolbar fills the entire screen, because the title is in the middle of the screen and the background of the toolbar is on the whole screen too. Here is the xml code:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
I've tried replacing the layout_height with 56dp and i've tried making a seperate layout file with the toolbar in it and include it into this xml, but that didn't work.
Why is my Toolbar filling the entire screen?
A DrawerLayout expects 2 Views in it. You supply 3.
The first view is the "background". It is the content of your screen. The second view, the drawer, is supposed to animate in and out.
You supply 3 views. So your toolbar is "the content"—thus fullscreen, and your drawer is your actual content.
To fix your issue, try something like this:
<DrawerLayout>
<!-- The main content view -->
<LinearLayout>
<Toolbar/>
<FrameLayout
android:id="#+id/content_frame"/>
</LinearLayout>
<!-- The navigation drawer -->
<ListView/>
</DrawerLayout>
DrawerLayout will accept only two child views. Try to add the Toolbar in frame layout.
Or you can create like beliw.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways" />
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/layout_drawer_header"
app:itemTextColor="#000000"
app:menu="#menu/global">
</android.support.design.widget.NavigationView>
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_drawer">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#color/octo"/>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- 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.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="am.octogr.app.octocopy.NavigationDrawerFragment"
tools:layout="#layout/main_drawer_listview" />
</android.support.v4.widget.DrawerLayout>
Hi, how can I put a Toolbar within this layout?
This layout for Navigation Drawer.
I've tried some code but still not working.
1. Under FrameLayout: Toolbar did not appear.
2. Below FrameLayout: Toolbar appear but content did not display.
3. Have DrawerLayout in a LinearLayout but my fragment seems didn't work well.
Thank you in advance.
Let me try to explain.
Android draws Views from top to down so; A-B-C- are you 3 respective Views in your DrawerLayout. now A being the Toolbar is drawn first with params width fit-to-the-overallwidth and height fit the content itself, B is drawn with width fit-to-the-overallwidth and height fit-to-the-overallheight, etc etc. that means your FrameLayout is drawn second in the drawing successions and on top of your Toolbar but since its covered you do not see that it is there, if you bring it below, your Toolbar is drawn second in succession hence you see it.
i hope you can deduct a solution from it now.
Have the toolbar in another layout which would be your main content view
<!-- The main content view -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#color/octo"/>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
Try this :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".main_drawer">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"
/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="am.octogr.app.octocopy.NavigationDrawerFragment"
tools:layout="#layout/main_drawer_listview" />
<!-- Your Content -->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
with a separate tool_bar.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/octo"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
Don't forget to Initialize your Toolbar and set it as the ActionBar:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
With a minSdkVersion of 14
Try this. It works fine for me.
Use this XML for your drawer activity.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="#layout/app_bar_layout" />
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_drawer"
app:menu="#menu/activity_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_layout.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.rsa.myapplication.DrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/> <!--This line is important-->
</android.support.design.widget.CoordinatorLayout>
you can find recent documentation about it on appbar.
They missed to include some code to make it work, here they are :
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
where R.menu.menu is your xml file in your project's res/menu/ directory. Paste above code on your activity which implement the Toolbar.
I'm trying to integrate a navigation drawer with my relative layout (which holds my activity content) and my toolbar. As you can see below, the toolbar is being duplicated within the interior relative layout as well as existing outside of it.
The highlighted portion is the relative layout.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/landing_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/frontPageBGColor">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<android.support.v4.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//Content Content Content
</RelativeLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:layout_height="match_parent"
android:layout_width="280dp"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
android:name="com.xxxxxxxx.chessgame.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
in your styles, you need to inherit the app theme from .NoTitleBar to remove the system provided action bar
like this
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
explained in detail here:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
Using the new Toolbar of Android 5 the icons appear cut off!
I'm using a drawer Navigation and the new toolbar. Any suggestions?
It seems that something it's above the toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
Here it is the Drawer 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"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/toolbar"/>
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:fitsSystemWindows="true"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/gray_light_divider_list_drawerNav"
android:dividerHeight="1dp"
android:background="#color/gray_light_background_list_drawerNav"
/>
Edit: Adding these two attributes in the toolbar
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
IT LOOKS TOO BIG
I added the toolbar twice, in the drawer navigation and also in the fragment itself
the height of the toolbar is to small. you should have your toolbar's minHeght as this
android:minHeight="?attr/actionBarSize"
The normal height of the Toolbar is 56dp. Try setting the height to that, it should look normal.
So my XML goes like this:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_height="56dp"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<FrameLayout
android:id="#+id/main_frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- ListView here -->
</android.support.v4.widget.DrawerLayout>
What is happening, even though I set my height explicitly as 56dp, the toolbar is acting like match_parent and wills the entire height of the screen? Is there a better way of doing this?
Or should I be putting the toolbar in side the layouts that my FragementTransactions fill the FrameLayout with? Which doesn't seem efficient because I have several of those.
DrawerLayout takes two children views: the first for the main content and the second for the drawer: both are always set to match_parent. Therefore your Toolbar and FrameLayout should be wrapped in a vertical LinearLayout which is set to match_parent as per the canonical example from the maker of AppCompat:
<!-- The important thing to note here is the added fitSystemWindows -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<FrameLayout
android:id="#+id/main_frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<!-- Your drawer view. This can be any view, FrameLayout
is just an example. As we have set fitSystemWindows=true
this will be displayed under the status bar. -->
<FrameLayout
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:fitsSystemWindows="true">
<!-- ListView here -->
</FrameLayout>
</android.support.v4.widget.DrawerLayout>