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.
Related
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
I want to create custom navigation drawer , but i have button and EditText in navigation . I use this to filter.but i cant create like this.
It's fairly easy to achieve customization
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--here is your navigation drawer just create what layout you want inside it-->
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<!--here you can create your custom view like this-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
after that you can simply defined the ID same we do for other view and set onclicklistener on them to get click event
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
I was going to use NavigationView instead of rolling my own in my DrawerLayout, but instead of using NavView's menu structure I am using my own ListView within the NavView tag. IOW, I do have an app:headerLayout in NavView but do not have an app:menu attribute.
This works as far as the drawer opening and closing and shows my list as expected with the two following glitches: it slides in below my ActionBar and the header is not displayed. The header is in the layout and I can find its IDs, but it is not shown.
So is what I am trying to do possible? The only real reason I am trying to use NavView is that it handles the drawer width properly, but I can live without that if I can't get the rest of it to display as I wish.
May be you can use listview inside the navigationView
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/main_toolbar" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"/>
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
you can not use app:header and app:menu as you are using listview. You have to create header manually.
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>