Right drawer marginTop not being set - android

I have a base activity with an layout defined like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_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"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<include
layout="#layout/app_bar_base"
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:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_base"
app:menu="#menu/activity_base_drawer"/>
<FrameLayout
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginTop="200dp"/>
</android.support.v4.widget.DrawerLayout>
The include part is where I set my toolbar and have a container that will receive a fragment.
Has you can see by the id, the FrameLayout will act as an right drawer.
I want that this drawer is placed bellow the toolbar.
I've tried to add an android:layout_marginTop to the drawer but with no success (that 200dp value is only for tests and the final value will be 56dp). When I open the drawer it takes all the screen height (bellow status bar).
Here is an image:
How can I put the frame layout bellow the toolbar?

Use a linear layout with a vertical orientation and place the toolbar and frame layout inside. Use "wrap_content" in the height property of the frame layout so that it will not take full height of its parent..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
layout="#layout/app_bar_base"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="200dp"/>
</LinearLayout>

Related

NestedScrollview not working with Drawerlayout

I am trying to add nested scroll view in my drawer layout but the nested scroll view is not working i.e. my view is not scrolling down. My content main lahout is supposed to be a scrolling layout. My layout contains a linear layout for toolbar and the content main for the home screen and a navigation view . Any type of help will be grateful. Here's my XML file code:
<androidx.drawerlayout.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"
tools:context=".Activities.MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
<include
layout="#layout/drawer_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
set NestedScrollView height to match_parent

Drawer layout setBackgroundColor does not work

I need to set the background color of my DrawerLayout at runtime. Here's what I'm doing:
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
drawerLayout.setBackgroundColor(Color.parseColor("#ffffff"));
I'm not really going to set it to a hard coded value, that's just for illustration. This seems to have no effect - the background stays a dark gray color that I think is picked up from a style somewhere. I can set the background color of the ExpandableListView inside the DrawerLayout, but I can't get that to fill the vertical space, so some of the background is the correct color and the empty space is the wrong color.
Setting the background color of the ExpandableListView in XML works, but I need to be able to change it at run time. If I don't set the ListView color in XML, the ListView will not fill the vertical space as I want it to do. It just cuts off after displaying all the items. I've tried setting both the drawer layout and list background in Java, and the closest I can come to having any effect is the empty space below the list items changes color. I cannot get the background of the actual list to change. Any suggestions?
Layout XML:
<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:orientation="vertical"
tools:context=".activities.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/primary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ToolBarStyle" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
<!-- The navigation drawer -->
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/drawer_background"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:groupIndicator="#null" />
</android.support.v4.widget.DrawerLayout>
Screen shot:
I think you need to set background for your NavigationView instead of DrawerLayout.
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white" //set background here
app:itemBackground="#android:color/transparent"
app:menu="#menu/menu_navigation"/>
If you use ExpandableListView inside DrawerLayout. I think you should put the ExpandableListView inside a RelativeLayout. Like this:
<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:orientation="vertical"
tools:context=".activities.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/primary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ToolBarStyle" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/drawer_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#drawable/background"> //set background here
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/drawer_background"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:groupIndicator="#null" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

Add scrollView in DrawerLayout having NavigationView and Toolbar

I want to create home page for my application. I want to have scrollview in the layout of the homepage. I tried adding scrollview inside DrawerLayout, but nothing helped. I kept the scrollview at various placed in DrawerLayout, but its not working. How do we do that?
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true">
<!--Main content-->
<LinearLayout
android:orientation="vertical"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/app_bar"/>
<ScrollView>
Other layouts and views here!
But it is not working!
</ScrollView>
</LinearLayout>
<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
android:id="#+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
What I get is
What I want is scroll view with all other functionalities intact. Some what like.

How to set navigation drawer right margin?

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.

Android DrawerLayout - No drawer view found with gravity

When I click on my drawer toggle I get the following exception:
java.lang.IllegalArgumentException: No drawer view found with gravity
LEFT
This is my activity_drawer.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"
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">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="#layout/fragment_navigation" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
My fragment_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
</ListView>
And my list item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/navigation_item_text"
android:layout_gravity="center_horizontal" />
</LinearLayout>
From documentation
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. Drawers commonly use match_parent for height with a fixed width.
<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"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<fragment
android:id="#+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="#layout/fragment_navigation" />
</android.support.v4.widget.DrawerLayout>
Ok. Let me make the things simple and clear here.
What is DrawerLayout?https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
DrawerLayout
Acts as a top-level container for window content that allows for
interactive "drawer" views to be pulled out from the edge of the
window. Drawer positioning and layout is controlled using the android:layout_gravity
attribute on child views corresponding to which side of the view you
want the drawer to emerge from: left or right. (Or start/end on
platform versions that support layout direction.) 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. Drawers commonly use match_parent for height with a fixed width.
What is DrawerLayout?, In Simple words:
The layout supported by Android that allows you to have an overlay screen, called as drawer, over your main screen.
DrawerLayout needs childviews to work, similar to the LinearLayout and RelativeLayout.
The childview must have either of the property set
android:layout_gravity="start" OR android:layout_gravity="left"
DrawerLayout uses this childView to know from where to start showing the Drawer i.e. From Left to Right or Right to
Left. There are countries where the text is read from Right to Left.
Techie Stuff:
EVENT#1: ActionBarDrawerToggle.java is set as the drawer listener for the DrawerLayout
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
toolbar,R.string.navigation_drawer_open,
R.string.navigation_drawer_close)
mDrawerLayout.setDrawerListener(mDrawerToggle);
EVENT#2: The user clicks on the Toolbar.navigationIcon that calls the toggle() function
ActionBarDrawerToggle.java (android-sdk\sources\android-22\android\support\v7\app\ActionBarDrawerToggle.java)
private void toggle() {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
Gravity.java (android-sdk\sources\android-22\android\view\Gravity.java)
/** Push object to x-axis position at the start of its container,
not changing its size.*/
public static final int START = RELATIVE_LAYOUT_DIRECTION | Gravity.LEFT;
EVENT#3: Toggle() function calls the mDrawerLayout.openDrawer(Gravity.START)
DrawerLayout.java (support-v4-22.1.1.jar library)
/*** Open the specified drawer by animating it out of view. **
#param gravity Gravity.LEFT to move the left drawer or Gravity.RIGHT
for the right. * GravityCompat.START or GravityCompat.END may also
be used. */
public void openDrawer(#EdgeGravity int gravity) {
final View drawerView = findDrawerWithGravity(gravity);
if (drawerView == null) {
throw new IllegalArgumentException("No drawer view found with gravity " +
gravityToString(gravity));
}
openDrawer(drawerView);
}
EVENT#4: If no childView of the DrawerLayout is set with the layout_gravity="start” or left the No drawer view found with gravity LEFT is thrown.
Solution:
<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"
android:fitsSystemWindows="true">
<!--This will appear when the drawer is closed (default view)-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</LinearLayout>
<!-- This will appear when the drawer is opened -->
<!-- the property,android:layout_gravity, is used internally to identify imageView as the view to be displayed when the drawer is opened -->
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height=" match_parent"
android:layout_gravity="left"
android:background="#drawable/img_shree_ganesha" />
</android.support.v4.widget.DrawerLayout>
I hope that helps. Happy Coding…
The "Android Drawer" needs to be a direct child node of the "DrawerLayout".
In your example above (re: original question example), you only have a single direct child node under "DrawerLayout" ("LinearLayout"), and no drawer view after it.
Move your drawer view out of your LinearLayout and place it after it.
in my case, because DrawerLayout attr: tools:openDrawer="start"
i added android:layout_gravity="start" for the second element
<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"
tools:openDrawer="right"
tools:context=".map.MapFragment">
<include layout="#layout/fragment_map" />
<FrameLayout
android:id="#+id/content_frame"
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="right"
android:background="#color/white"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/drawer_nav_header"/>
</android.support.v4.widget.DrawerLayout>
add
android:layout_gravity="start"
to NavigationView
like this
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="#layout/drawer_menu"
android:layout_gravity="start"
android:foregroundGravity="right"
android:id="#+id/nv">
<include layout="#layout/drawer_menu"/>
</com.google.android.material.navigation.NavigationView>
Are you using right to left(RTL) layout? Setting gravity left on RTL layout would throw this exception. This can be fixed by setting gravity start instead of left
I solve this problem like this :
XML :
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
android:layoutDirection="rtl"
tools:openDrawer="end"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include layout="#layout/content_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layoutDirection="rtl"
android:background="#color/white"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/menu_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
AND IN JAVA:
if (mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END);
}
I hope this helps someone!
You should use the same gravity in DrawerLayout and NavigationView: for example: tools:openDrawer="right"in DrawerLayout tag and android:layout_gravity="right" in NavigationView tag
<?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"
tools:openDrawer="right"
tools:context=".map.MapFragment">
<include layout="#layout/fragment_map" />
<FrameLayout
android:id="#+id/content_frame"
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="right"
android:background="#color/white"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/drawer_nav_header"/>
</android.support.v4.widget.DrawerLayout>
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
SOLUTION
Assign a layout_gravity = "start" or "left" attribute to one of your DrawerLayout child view if your drawer layout already have a child view. OR
Simply create a child view inside your DrawerLayout View and give it a layout_gravity = "start" or "left" attribute.
for example
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/mDrawer_Layout"
tools:context="com.rad5.matdesign.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<!-- Notice that here an image view as a child of the Drawer layout was -->
<!-- given a layout_gravity="start" -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.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.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_navigation_items" />
</android.support.design.widget.CoordinatorLayout>
The error fixed after replacing
<fragment
android:id="#+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.MyFragment"/>
with
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
and added Fragment instance to the FragmentContainerView in onCreate()
supportFragmentManager.beginTransaction()
.add(R.id.fragmentContainerView, MyFragment())
.commit()

Categories

Resources