Navigation View Multiline Text - android

I have an app with a Navigation View for my DrawerLayout. I add programmatically items in the menu because I receive the information over the network. But sometimes, an item name can be very long and is just cut off with even having the ellipsize icon "..."
Does someone have an idea on how to have multiline for my menu items?
Thanks

Override design_navigation_menu_item.xml from the Android Support Design Library and modify the things you need (set android:ellipsize="end" and android:maxLines="2")
Your res/layout/design_navigation_menu_item.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<CheckedTextView
android:id="#+id/design_menu_item_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawablePadding="#dimen/design_navigation_icon_padding"
android:gravity="center_vertical|start"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:ellipsize="end"
android:maxLines="2" />
<ViewStub
android:id="#+id/design_menu_item_action_area_stub"
android:inflatedId="#+id/design_menu_item_action_area"
android:layout="#layout/design_menu_item_action_area"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</merge>
However, you should just ellipsize the text to properly follow the Material Design guidelines.
You have not to override anything to just ellipsize the text:
Add a new style to the styles.xml
Set the new style to the NavigationView
res / values / styles.xml
<style name="TextAppearance">
<item name="android:ellipsize">end</item>
</style>
res / layout / activity_main.xml
<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"
app:theme="#style/TextAppearance" />

Just add app:itemMaxLines="2" attribute on the NavigationView tag like this:
<com.google.android.mayerial.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"
app:itemMaxLines="2" />
NOTE: Material dependency is--
implementation 'com.google.android.material.material:1.0.0'

Add a style to your NavigationDrawer and then include 2 lines in the style.
<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:background="#drawable/estilo"
android:fitsSystemWindows="true"
app:itemTextColor="#color/white"
app:theme="#style/NavigationDrawerStyle"
app:menu="#menu/activity_main_drawer" />
<style name="NavigationDrawerStyle">
<item name="android:lines">2</item>
</style>

Check out this answer. Default android slide menu is not very flexible, most likely you will have to create your custom view.
I really this tutorial from AndroidHive.

I had the same problem and I tried and tried, There was no progress with Menu, I don't say it was impossible but I couldn't find a way to break MenuItem lines.
But if you don't insist on using Menu I suggest you to use a ListView or RecyclerView in your layout 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"
app:headerLayout="#layout/nav_draw_header">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_marginTop="160dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.NavigationView>
As you can see there is no special thing to explain, no menu, just a RecyclerView, layout_marginTop value is same as header height, I'm sure you know the rest of the story (How to fire up RecyclerView), This approach gives more flexibility to NavigationView menu items (actually RecyclerView items).
If my answer is not clear enough to you let me know.
Here is the result:

You shouldn't change that.
Check out the material design guideline: https://material.io/design/components/navigation-drawer.html#anatomy

Add a style in your NavigationDrawer that declares the number of lines as 'two', like so:
<style name="NavigationDrawerStyle">
<item name="android:lines">2</item>
</style>
and in your view:
<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:background="#drawable/estilo"
android:fitsSystemWindows="true"
app:itemTextColor="#color/white"
app:theme="#style/NavigationDrawerStyle"
app:menu="#menu/activity_main_drawer" />

Related

How do I add a paddingBottom to a drawer layout in android?

I am working on an android project that uses a drawer layout. The drawer layout contains many items and is therefore scrollable. The problem is, I need the last item to have a padding bottom such that there will be some spacing after scrolling to the last item.
From the screenshot above, how can I add some padding between the last item 'Log out' and the end of the screen.
I have tried adding padding bottom to the drawerlayout but its still not working
<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"
android:paddingBottom="#dimen/_120sdp"
android:clipToPadding="false">
I have also tried enclosing navigation view in a scrollview but it is also not working
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="#dimen/_120sdp">
<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" />
</ScrollView>
This is my last element in the drawer layout
<item
android:paddingBottom="#dimen/_190sdp"
android:title="#string/action">
<menu>
<item
android:id="#+id/nav_rate_us"
android:title="#string/rate_us" />
<item
android:id="#+id/nav_log_out"
android:paddingBottom="#dimen/_120sdp"
android:title="#string/log_out" />
</menu>
</item>
How can I achieve this?
add one dummy items to the end of your menu resource file.
<item
android:title=""
android:enabled="false" >
</item>

I want to change the icon in the navigation view

<item
android:id="#+id/nav_day"
android:checkable="true"
android:icon="#drawable/ic_weekly_calendar"
android:textColor="#ff00ff"
android:title="Day Planner!" />
I want to know how to change the color of the icon of the menu item I get from the navigation view. I know that you can use ItemIconTint to change at once.
I want to use the original color of the icon as it is. What should I do?
<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="#color/colorPrimary"
app:itemIconTint="#android:color/primary_text_dark"
app:itemTextColor="#android:color/white"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
/>
yourNavigationView.setItemIconTintList(null);
You can check the duplicate question post here:
Disable icon colorStateList in NavigationView

How to apply selector for selected item in navigation drawer Android?

I am using navigation activity in Android studio. I have tried code below:
<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:background="#drawable/sidebar_bg"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/color_white_transperntdark"
app:itemTextColor="#color/color_white_transperntdark"
app:menu="#menu/activity_main_drawer" />
But below I need a kind of selector having some color and white vertical line. So give me a hint:

how do i assign a different backgroundcolor to all the items in a group in navigation drawer?

I have three groups in my NavigationDrawer, two of which have to stay white and the other one has to become gray.
I have seen many people say that it would be better to use a ListView to do this but I wonder if its possible with a menu?
You can provide the background color to your navigationView like below:
<android.support.design.widget.NavigationView
android:id="#+id/adcl_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/app_white"
app:headerLayout="#layout/drawer_header_layout"
app:itemIconTint="#color/app_secondary_text"
app:itemTextColor="#color/app_secondary_text"
app:menu="#menu/menu_drawer"
app:theme="#style/ThemeOverlay.AppCompat.Light" />
And if you want to change Single item background color then use:
<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:itemBackground="#drawable/my_ripple"
app:itemIconTint="#2196f3"
app:itemTextColor="#009688"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_view" />

Change Navigation Drawer Icon and Text Spacing

I am wondering how do we change the spacing between the icon and the text?
Is it doable?
Any thoughts?
Thank you
Add the following line to app\src\main\res\values\dimens.xml file. No need to add new layout.
<dimen tools:override="true" name="design_navigation_icon_padding">5dp</dimen>
I added one line to xml: app:itemIconPadding="#dimen/space24" and it`s worked
<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"
android:clipToPadding="false"
android:paddingBottom="#dimen/space48"
app:headerLayout="#layout/layout_nav_header"
app:insetForeground="#color/black"
**app:itemIconPadding="#dimen/space24"**
app:itemBackground="#drawable/background_selected_menu"
app:itemHorizontalPadding="#dimen/horizontal_padding_on_nav_drawer"
app:menu="#menu/drawer">
If you change in your dimens.xml file you'll change on all of your project. I suggest you to change locally, just in NavigationView, like Vadym purpouse. Or you can change there like that:
<com.google.android.material.navigation.NavigationView
app:itemIconPadding="4dp"
../>
This worked perfectly to me, and you can just change the value of the padding to everthing that you want.
You will need another layout which you will inflate. In the layout, you can set the margin to what you want. Inflater allows you to adapt a layout to a view. check this out http://developer.android.com/reference/android/view/LayoutInflater.html
or this
http://khajanpndey.blogspot.com.ng/2012/12/android-layoutinflater-tutorial.html
Whichever view you are using, checkout how you can inflate the view.
Hope this help.
Here is the solution -
app:itemIconPadding="15dp"
Example:
<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="false" app:itemIconPadding="15dp" app:itemIconSize="28dp" app:headerLayout="#layout/nav_header_main2" app:itemIconTint="#color/navigation_item_text_color" app:menu="#menu/activity_main_drawer" app:itemTextAppearance="#style/NavigationDrawerStyle" app:itemBackground="#color/white" />

Categories

Resources