How do I get my main layout to simply line up directly underneath my DrawerLayout? My main layout is listed below and is mainly made up of listviews. Right now everything is overlapping. So the below first shows my DrawerLayout. After closing of DrawerLayout my main activity is listed.
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name=“com.example.my.app.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent”
android:orientation="vertical">
<TextView
android:id="#+id/txt_empty_list_cars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="4dp"
android:gravity="center"
android:text="#string/view1"
android:textAppearance="?android:attr/textAppearance"
android:textColor="#android:color/darker_gray"
android:visibility="gone" />
<ListView
android:id="#+id/list_cars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_header"
android:layout_margin="4dp"
android:divider="#android:drawable/divider_horizontal_bright"
android:dividerHeight="0.5dp"
android:listSelector="#android:drawable/list_selector_background" />
So, your problem is drawer layout overlapping the linear layout below. Use
android: layout_below="#id/drawer_layout"
In your linear layout. That will linearly arrange both layouts.
I dont know if your are looking for a sliding panel like the new gmail app
but if yes you can refer yourself Sliding Pane Layout and this very awesome tutorial.
hope it helps, happy codings.
In order to use the DrawerLayout correctly, it has to be the only root / parent view of your entire layout.
<android.support.v4.widget.DrawerLayout>
/*other nested child views */
<fragment
android:id="#+id/fragment_navigation_drawer"
...
/>
</android.support.v4.widget.DrawerLayout>
Also, to prevent the over lapping, ensure that you have two direct child views as per design guideline at
https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
"To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent . Add drawers as child views after the main content view and set the
layout_gravity appropriately."
Related
I have a layout for an Activity that I'm trying to add a navigation drawer to.
The problem is, to work properly, I need to use:
<android.support.v4.widget.DrawerLayout
instead of:
<RelativeLayout
but it messes things up. My ProgressBar becomes much bigger, my RecyclerView doesn't work, the app logs me out when I click something, etc.
My layout XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.st.mf.UserAreaActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="#dimen/activity_vertical_margin"
android:background="#fff">
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#layout/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
How can I create my drawer menu without messing everything else up?
Any direct child View of a DrawerLayout that's not a drawer is considered a content View, and will be laid out to match_parent in both directions, regardless of the width and height attributes you've set on it. In your case - indeed, in most cases - you only need one content View, so the rest of the non-drawer Views should all be inside a single ViewGroup.
We'll place your ProgressBar and RecyclerView both inside a RelativeLayout that acts as the content View, where they'll keep the layout attributes you've set. For example:
<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"
tools:context="com.st.mf.UserAreaActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_vertical_margin"
android:background="#fff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#layout/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Note that the content View should always be listed before any drawers, to maintain proper z-ordering; i.e., to keep the drawers on top of the content.
As per title, how can I set the right margin of the navigation drawer to 56 dp as per Google's material design (refer below)? I have tried android:layout_marginRight="56dp" in the second view of the DrawerLayout but it looks very weird (more than 56dp and very different from google's material design picture below).
I would like to suggest that you focus on the width of the Second View of DeawerLayout which should be within 240dp to 320dp,
Think about this you manage to give navigation drawer right margin of 56 dp on the right side, now you want to check this in landscape mode, and more surprises will be waiting for you
Try focus on the width of the navigation drawer, it should be in between 240dp to 320dp. It will automatically adjust with the right margin.
This is an example of the xml file which contains the navigation drawer fragment. Here the width of the fragment is set in the dimens folder as 280dp
<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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<it.neokree.materialtabs.MaterialTabHost
android:id="#+id/materialTabHost"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="#+id/app_bar"
app:accentColor="#color/colorAccent"
app:hasIcons="true"
app:iconColor="#android:color/white"
app:primaryColor="#color/colorPrimary" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/materialTabHost"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.example.fragments.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Now this is an example which is the layout of the navigation drawer
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context="com.example.fragments.FragmentDrawer">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
It will be a good practice if you set the width in the dimens folder rather than hardcoding it.
I have a DrawerLayout with a Coordinator layout hosted as the main content. When I create a SnackBar with the Snackbar.make method, the FAB hosted in the Coordinator layout refuses to animate.
I find this odd, because I've used the same FAB in Coordinator layouts that aren't wrapped in a DrawerLayout and it animates just fine, leading to believe the DrawerLayout is somehow blocking the call back.
I've tried making the CoordinatorLayout the top level view, wrapping the DrawerLayout, but that also doesn't work. What I'm going to attempt is forking the FloatingActionButton Behavior class and making the updateFabTranslationForSnackbar method public so I can call it myself. I'd much rather not do this, so any ideas will be much appreciated.
For both activities, the Snackbar.make call is called from a fragment added dynamically to the RelativeLayout with the ID "container". The view passed to the call is the CoordinatorLayout in the activity XML with the id "coordinator_layout".
Again, everything works as it should in the first XML, but not in the second.
This is my default XML for other activities, the FAB animates fine here:
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:name="com.app.mobile.app.ui.BusinessActivityFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_sign_up"
android:visibility="invisible" />
<View
android:id="#+id/background_mask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#color/black_40"
android:visibility="invisible" />
</RelativeLayout>
<include
android:id="#+id/action_bar"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_gravity="top" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp" />
<ProgressBar
android:id="#+id/load_more"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/half_margin"
android:layout_gravity="bottom"
android:indeterminate="true"
android:visibility="gone" />
This is the XML for my main activity where the FAB refuses to animate:
<android.support.v4.widget.DrawerLayout
android:id="#+id/navigation_layout"
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"
tools:context="com.app.mobile.app.ui.HomeActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<!-- Toolbar is the last item in the FrameLayout to cause it to overlay -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<ProgressBar
android:id="#+id/home_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginBottom="8dp" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
<include
android:id="#+id/action_bar"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_gravity="top" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp" />
<ProgressBar
android:id="#+id/load_more"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/half_margin"
android:layout_gravity="bottom"
android:indeterminate="true"
android:visibility="gone" />
</android.support.design.widget.CoordinatorLayout>
<!-- 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:name="com.app.mobile.app.ui.NavigationDrawerFragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:tag="NAVIGATION_DRAWER_TAG"
tools:layout="#layout/fragment_navigation_drawer" />
Make sure you are passing the right view to the Snackbar.make() method. As per the docs,
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
So you should pass a view that is actually inside the CoordinatorLayout, so that Snackbar will find the coordinator as the first parent available.
I found the solution. You apparently must move to the Navigation view widget in the new design library. Moving to this and keeping all else constant, the FAB animates again.
For implementation see The official blog post for the support libary and this demo of the library by Chris Banes.
This is not an ideal fix, as you lose the flexibility of a fragment to a static inflated XML resource, but it's the only way I've found to make the FAB still animate.
I am using a progress bar inside drawer layout. When I start the app the progress bar is always aligned to top left corner of screen. I tried to set android:layout_gravity property but still It is shown in top left corner. Below is my xml layout file.
<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=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
style="?android:attr/progressBarStyleLarge" />
<ListView
android:id="#+id/mainListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:dividerHeight="0dp"
android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>
use android:gravity="<whatever>" on the FrameLayout, since you only have the progressbar inside it.
Also, your drawer is going to be the listview because that's what has the layout_gravity="start" property set. If you want the progress bar to be within the left-drawer you can wrap the progress bar and listview into a container and set the layout_gravity="start" on that.
without further explanation, that's all i can aid with.
It's work for me. In FrameLayout set
android:layout_centerInParent="true"
Please try this code, This code is work for me.
ProgressBarView.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_gravity="center"
android:orientation="vertical">
</ProgressBar>
Now include this layout into NavigationView,
<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">
<include layout="#layout/progress_bar"/>
</android.support.design.widget.NavigationView>
Preview in Studio
I've been trying to make a DrawerLayout that has a ViewPager and a LinearLayout attached to the bottom of the DrawerLayout. The problem is that the ViewPager and LinearLayout are overlapping at the top of the view, and all my efforts to move the LinearLayout down have failed so far.
The XML Layout:
<?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.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/tabstrip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/base_activity_viewpager"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textIsSelectable="false" />
</LinearLayout>
<!-- Naviagtion drawer -->
<ListView
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
I've tried adding several things to linear_layout:
android:layout_gravity="bottom" (causes an IllegalStateException)
android:layout_below="#id/viewpager"
android:layout_alignparentbottom="true"
But all of these values seem to be ignored, even changing android:layout_width of the LinearLayout is ignored. I think i'm overlooking something obvious but if anyone has suggestions on how to do this it would be greatly appreciated.
A drawerlayout is allowed only 2 children, the first is a main content view, the second is the drawer. You need to combine your first two views into a linear layout. http://developer.android.com/training/implementing-navigation/nav-drawer.html