Android NavigationView set menu from XML programmatically - android

How do I create Android Navigation View menu from XML programmatically?

Using design support library you can add Navigation view easily.
Add below code in xml:
<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>
and then create a menu file for navigation

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.

How set transparency onleft side menu in NavigationDrawerActivity

I am creating default Navigation Drawer Activity for my project. I would like set my left navigation bar transparent. Do you have any ideas how to do it?
change the the background code of NaviagtionView and him transparent
<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"
android:background="#android:color/transparent"
app:headerLayout="#layout/nav_header_main3"
app:menu="#menu/activity_main3_drawer" />

Custom Listview in Navigation Drawer

How can I customize a ListView in a Navigation Drawer, like the one in the 2nd picture here.
You can use NavigationView of Android Design Support Library (see: Android Developers Blog).
With NavigationView, you don't have to use ListView any more. You can populate the drawer menu simply with menu.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:fitsSystemWindows="true">
<!-- your content layout -->
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

Categories

Resources