<layout_gravity="start"> doesn't work on NavigationView? - android

I'm trying to make a Navigation Drawer, and inside the NavigationView I've added the layout_gravity="start" attribute, but it wont hide the View. this is my code:
<RelativeLayout ... >
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"></android.support.design.widget.NavigationView>
</RelativeLayout>
What could be wrong? Thanks.

If you intend for the NavigationView to behave as a drawer, then the root View should be <android.support.v4.widget.DrawerLayout>, instead of <RelativeLayout>

I hope this works for you because layout_gravity cannot be used inside Relativelayout.
<androidx.drawerlayout.widget.DrawerLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"></android.support.design.widget.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

Related

How to display an activity or fragment on navigationView in Android?

In Swift, we can use a Library called "SideMenu" to show a viewController on the SideMenu.
Now, I'm coding in Android. I want to display activity in the NavigationView we can only display a Menu or header.
I cannot show an activity or fragment on the NavigationView.
Can you help me or give me some advice about this problem?
ANDROID STUDIO NAVIGATION VIEW
XCODE SWIFT SIDE MENU
Android app can display one activity at once (it's changed recently but in your scenario it's still true). In your case you should inflate View or Fragment. Just put your layout or fragment inside NavigationView:
<?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"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.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_main"
app:menu="#menu/activity_main_drawer">
<TextView
android:text="Custom View"
android:layout_gravity="center"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
NavigationView extends FrameLayout so if you will inflate menu it will overlap. Because of that you may want to delete:
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
If you are using the android navigation component you can see the full detailed information to achieve this and replacing the fragments to corresponding to user clicks/choices drawer layout with navigation component
if you aren't using the android navigation component and just using the drawer layout you can see this drawer layout

Navigation View Below DrawerLayout

hi i have a navigation view with a drawer layout i want to looks like but mine is look like
and this is my code
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<include layout="#layout/activity_multi_tab"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="60dp"
app:headerLayout="#layout/header"
app:menu="#menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
Shift your navigation view inside the DrawerLayout but below the included layout.
Navigation view has to be the second child of DrawerLayout
Also change the layout gravity to start.
android:layout_gravity="start"
And remove the margin top. There'll otherwise be a space at the top
Refer this official documentation for more details on the view
https://developer.android.com/reference/android/support/design/widget/NavigationView.html

Use image as item in menu for Navigation Menu Drawer

I'm creating a project with Navigation Drawer Menu in Android, however, so far the menu only shows an icon and title in the menu.
How do I change the whole menu item to become an image with text inside?
Currently: the menu is:
However, I would prefer it to be like this:
You should use listview or recycler view in your NavigationView like this:
<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:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/Mainheader"
layout="#layout/nav_header" />
<ListView
android:id="#+id/exp_navigationmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary" />
</LinearLayout>
</android.support.design.widget.NavigationView>
After that, set data in Listview.
You may use NavigationView#addHeaderView(View) API and provide your custom view as a content of NavigationView.
The alternative is to use app:headerLayout="#layout/your_layout" through xml.

remove or hide header navigationView in android

i am using design 23.1.1 in my project. i want drawer without header. just menu items are enough. i dont add any header view(programmatically or in XML). but in drawer i have empty header. please help me how to remove this empty header.
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:menu="#menu/drawer"
/>
View headerView= LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(headerView);
navigationView.getHeaderView(0).setVisibility(View.GONE);
i use dummy layout. and solved my problem but i think it is ridiculous.
If You want to remove header:
navigationView.removeHeaderView(navigationView.getHeaderView(0));
Of cource if You have more haeders You have to do this in some loop to remove them all. 0 is index of the first one.
try to add this to your style file <item name="android:windowFullscreen">true</item>
and remove app:headerLayout from navigationView xml.
and add this to your navigationView xml :
android:layout_marginTop ="#dimen/abc_action_bar_default_height_material"
it worked perfect for me.
To remove it permanently:
The Layout that contains the NavigationView widget is like this:
<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_main"
app:menu="#menu/activity_main_drawer"/>
Just remove the app:headerLayout line from it:
app:headerLayout="#layout/nav_header_main"

custom layout in navigation drawer

I am new to android, I follow this tutorial to create Navigation Drawer here
I make the navigation drawer successfully but I dont know how to change framelayout to my custom layout with button, textview....My app just show menu and blank framelayout, how can I solve this ?
<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" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="asdsadsad" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/purple_dark"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="0.1dp"
android:listSelector="#drawable/ic_drawer"/>
</android.support.v4.widget.DrawerLayout>
you can use a RelativeLayout on LinearLayout instead of FrameLayout and you can build your whole layout inside that
If you need to add another layout you need to gie it to your closeDrawerand drawerOpen.
Please refer my answer here

Categories

Resources