Android Navigation View Align Menu Items - android

In my app i have a nvigation view with some menu items. I want to align some of them to the center of the view. How can i achieve this correctly?
This is what i want:
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/nav_camara" android:icon="#android:drawable/ic_menu_camera"
android:title="Import" />
<item android:id="#+id/nav_gallery" android:icon="#android:drawable/ic_menu_gallery"
android:title="Gallery" />
<item android:id="#+id/nav_slideshow" android:icon="#android:drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item android:id="#+id/nav_manage" android:icon="#android:drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item android:id="#+id/nav_share" android:icon="#android:drawable/ic_menu_share"
android:title="Share" />
<item android:id="#+id/nav_send" android:icon="#android:drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
Thanks in advance.

As far as I know this is not possible in easy way.
You could find or navigate through views hierarchy to find views or textviews you want to center.
But i think NavigationView is not designed to align things other than left or right

Related

activity_home_drawer.xml seems to be empty

MY activity_home_drawer.xml
activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_camera"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
<item
android:id="#+id/nav_tools"
android:icon="#drawable/ic_menu_manage"
android:title="#string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="#string/menu_send" />
</menu>
</item>
</menu>
I was following a tutorial in YouTube about how to create an App Navigation drawer for my android app, while following the procedures, that occurred (see photo above), I'm new to android programming so, can anybody please tell me what's the root cause of this problem and how to fix it? Thanks.
remove this line
tools:showIn="navigation_view"
If we look into the official documentation, it clearly states that the tools:showIn is,
Intended for: Any root <View> in a layout that's referred to by an
<include>
Basically, tools:showIn lets Android Studio's layout editor to know where the View that uses it should be placed.
From the above explanation we can figure out that tools:showIn,
should be used by a Root <View>
the passing layout should refer this view by an <include>
Now, if you look into your source code, you will observe that,
You are using tools:showIn in your menu which is not it is intended for
You did not <include> the activity_home_drawer.xml in your navigation_view layout. Though you will not be able to do so, as it's not a <View>.
For the above mention reason, Android Studio is not able to preview the menu. The workaround to fix this issue is to remove the tools:showIn attribute and the tools namespace as well.
Remove
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
After refactoring your activity_home_drawer.xml menu should look like,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_camera"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
<item
android:id="#+id/nav_tools"
android:icon="#drawable/ic_menu_manage"
android:title="#string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="#string/menu_send" />
</menu>
</item>
</menu>

Android drawer selection and scrolling with submenu

I have two problems with the drawer submenu (I use the drawer example generated with the android studio wizard)
First problem
I try to select de subitems, for example if I click on "Tools" the selecteItem "effect" its works fine, but when I try to click on "Share" or "Send" and the selectedItem "effect" is not works:
(This is crazy because is the Android example, I suppose that this is an official example and should be work fine)
Second problem
I have another example wich have a lot of items (for example 15 items with subitems), so when i click on the last, after that, when I try to open de drawer, this is automatically scroll to top. son I can not see the last item selectet. I need that the scroll works automatically to the selected item. Is this possible?
Example code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
for your first problem just add group to your items android:checkableBehavior like first item group
<item android:title="Communicate">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</group>
</menu>
</item>
and for second problem as for as i know it maintain selected item automatically check this after changing the android:checkableBehavior

How can I Modify default images of Navigation Drawer.?

I want to change default Images of Navigation Drawer in Android.When I'm changing it's not working any one help me please.Thanks stack over flow.how can I do this help me please.
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
I hope you need some pre defined vector drawables as one of your comments was pointing to it.
Try
Right Click drawable folder
New -> Vector Asset
Enable Material Icon RadioButton (enabled by default)
Click Choose button
Select one image. change Resource name if required & click Next -> Finish.
Now a new vector drawable will be created in drawable folder & you can refer it using #drawable.
Here is my .xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_home"
android:title="section1"
android:checked="true"/>
<item
android:id="#+id/nav_add_event"
android:icon="#drawable/ic_menu_addevent"
android:title="section2" />
<item
android:id="#+id/nav_managenews"
android:icon="#drawable/ic_menu_manage_news"
android:title="section3" />
<item
android:id="#+id/nav_leaderboard"
android:icon="#drawable/ic_menu_leadre_board"
android:title="section3"/>
<item
android:id="#+id/nav_mypoints"
android:icon="#drawable/ic_menu_mypoint"
android:title="section4" />
<item
android:id="#+id/nav_myprofile"
android:icon="#drawable/ic_menu_my_profile"
android:title="section5" />
<item
android:id="#+id/nav_change_password"
android:icon="#drawable/ic_change_password"
android:title="section6"/>
</group>
</menu>

changing icon of navigation drawer items

When I changing icon in navigation drawer items by using (drawable > image asset > 'Action bar and tab icon') or (copy + past ) in drawable folder, in app menu icon shows the 'Theme color' witch was selected form " image asset > 'Action bar and tab icon' > Theme ".
I tried to all the three theme but main icon can not be shown
Here the apps screenshot and activity_main_drawer.xml file and xml file screenshot
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item android:id="#+id/nav_us"
android:icon="#drawable/eee"
android:title="US" />
<item android:id="#+id/nav_world" android:icon="#drawable/bbb"
android:title="World" />
<item android:id="#+id/nav_tech" android:icon="#drawable/f_g_h"
android:title="Tech" />
<item android:id="#+id/nav_sports" android:icon="#drawable/f_g_h"
android:title="Sports" />
<item android:id="#+id/nav_worl" android:icon="#android:drawable/ic_dialog_dialer"
android:title="World" />
<item android:id="#+id/nav_tec" android:icon="#android:drawable/ic_dialog_dialer"
android:title="Tech" />
<item android:id="#+id/nav_sport" android:icon="#android:drawable/ic_dialog_dialer"
android:title="Sports" />
</group>
<item android:title="Communicate">
<menu>
<item android:id="#+id/nav_share" android:icon="#android:drawable/btn_star"
android:title="About us" />
<item android:id="#+id/nav_send" android:icon="#android:drawable/ic_menu_send"
android:title="Contact us" />
</menu>
</item>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
Please have a look like this.

Main category icons in navigation drawers not visible

below is my xml code where vector image works on items but does not work on its parent. I have given comment for those two parents.I also wish to give colored icons there later and textcolor white.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
</group>
<item android:title="Communicate"
android:icon="#drawable/ic_menu_share">
<!-- above image not visible but below visible-->
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
<item android:title="Setting"
android:icon="#drawable/ic_menu_share">
<!-- above image not visible but below visible -->
<menu>
<item
android:id="#+id/nav_share1"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send1"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
Here "Communicate " and "Setting" not showing icons
Put them in menu if you want to show icons also:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
</group>
<menu>
<item android:title="Communicate"
android:icon="#drawable/ic_menu_share">
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
<menu>
<item android:title="Setting"
android:icon="#drawable/ic_menu_share">
<item
android:id="#+id/nav_share1"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send1"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
You can change the textcolor of all menu item using: app:itemTextColor="#android:color/white" attribute in xml code of navigation view.
Edit
You will get the colored icons if you use either
app:itemIconTint="null" in xml or
navigationView.setItemIconTintList(null); in your java code.

Categories

Resources