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>
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'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.
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.
I am using Androidhibe NavigationDrawer and SwipeTab with Custom Action Bar as seen here:
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
The NavigationDrawer is shown over the ViewPager (below the ActionBar) but I would like it to show on top of the ActionBar. How can I achieve that affect?
You can use Toolbar instead of ActionBar to achieve that.
Here is a blog post that may help you from switching over to Toolbar.
The XML for your MainActivity would look something like this:
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"/>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<!-- 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:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.app.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
The XML for my Toolbar looked like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimary"/>
I struggled with it at first, but once I switched over to using Toolbar the Navigation Drawer overlayed the Toolbar just like I wanted. If the blog post and sample code here is not enough, just let me know and I can continue to help you through it.
How can I create a navigation drawer like Google+ where there is a header for user profile info and a listview below?
I have the following code in my Activity:
<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="com.br.app.MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.br.app.NavigationDrawerFragment" />
</android.support.v4.widget.DrawerLayout>
Currently, the NavigationDrawerFragment contains just a listview, but I tried to add a RelativeLayout as a container with an inner RelativeLayout to create the profile area and the listview below, but the app is crashing.
I've read the tutorial from but as I could understand, my header would be an item of the listview, and it won't be fixed on top (without scrolling).
How can I create this custom navigation drawer like Google+?
Actually, there are two version of navigation drawer used by google.
the first is the old navigation drawer BELOW the action bar. It is used in Google Play Store and Google Books for example.
HERE you can find a library that implements it.
The second is the new navigation drawer that is showed in Material Design guidelines where the drawer is over the action bar.
HERE you can find a library that implements it.
Actually these two libraries are in developing. But they are almost usable at this time.
You can easily customize the android Navigation drawer once you know how its implemented. here is a nice tutorial where you can set it up.
This will be the structure of your mainXML:
<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 to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>