Change specific icon's color in NavigationDrawer - android

I have a problem with changing the icon's color in default Drawer Menu in Android, there are lots of topics but they only explain changing the tint color of whole menu not a specific icon:
For example I want to change the color of Favorites icon to yellow

You can change icon of the menu from here like this from activity_drawer_drawer.xml layout
<?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"
<!-- Change your drawable here -->
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>
To remove tint color effect use this
mNavigationView.setItemIconTintList(null);

Related

Adjust the margin of group title in navigation drawer

How can I decrease the margin of the menu headings as shown in the image. I am using the menu to inflate the items in the drawer. Just want to decrease the top and bottom margin from the menu headings. Here is nav_drawer_items.xml
<item
android:title="MAIN"
android:id="#+id/tabs"
android:checkableBehavior="single">
<menu>
<item
android:id="#+id/nav_home"
android:title="#string/nav_home"
android:icon="#drawable/home_icon"/>
<item
android:id="#+id/nav_movies"
android:title="#string/nav_movies"
android:icon="#drawable/genre_icon"/>
<item
android:id="#+id/nav_originals"
android:title="#string/nav_originals"
android:icon="#drawable/genre_icon"/>
<item
android:id="#+id/nav_live_tv"
android:title="#string/nav_live_tv"
android:icon="#drawable/genre_icon"/>
</menu>
</item>
<item android:id="#+id/others"
android:title="SUPPORT">
<menu>
<item
android:id="#+id/nav_about_us"
android:title="#string/nav_about_us"
android:icon="#drawable/genre_icon"/>
<item
android:id="#+id/nav_feedback"
android:title="#string/nav_feedback"
android:icon="#drawable/genre_icon"/>
<item
android:id="#+id/nav_setting"
android:title="#string/nav_setting"
android:icon="#drawable/genre_icon"/>
</menu>
</item>
You can change the group title padding by adding the following line to your dimens.xml file
<dimen tools:override="true"
name="design_navigation_separator_vertical_padding">0dp</dimen>

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>

Change NavigationView divider color and sub header text color

I im using the android studio navigationview.
But i change the navigationview color background to black,
and the divider for subheader gone(because the divider is also black).
Also the subheader title is gone.
Is there a way to change those properties to white or any color?
Here is my XML for my menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/nav_header"
android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black"
android:title="Search Location" />
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_favorite"
android:title="Favorites" />
<item
android:id="#+id/nav_recent"
android:icon="#drawable/ic_nav_route"
android:title="Recent Location" />
<item
android:id="#+id/nav_route"
android:icon="#drawable/ic_place"
android:title="Route" />
</group>
<item
android:id="#+id/navigation_subheader"
android:title="Others">
<menu>
<item
android:id="#+id/nav_settings"
android:checkable="true"
android:icon="#drawable/ic_settings"
android:title="Settings" />
<item
android:checkable="true"
android:id="#+id/nav_about"
android:icon="#android:drawable/ic_menu_send"
android:title="About" />
</menu>
</item>
</menu>
Thanks!
For the separator you need to add style to your theme or NavigationView theme and change the value of android:attr/listDivider. Thats the background that divider is using.
Subheader is just a TextView. When you change the color of the divider you will see that it changes to the subheader. To change subheader text change the android:textColorPrimary and android:textColorSecondary in v21 value in your style too.

Android Navigation View Align Menu Items

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

How to keep single checkableBehavior mode in drawer menu for NavigationView when we add section?

I try to implement a drawer with new component of material design : NavigationView.
It's work very well. When I select an item changes its color change well with android:checkableBehavior="single".
<group
android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/home"/>
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
...
<item
android:id="#+id/drawer_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/settings"/>
</group>
The problem come when I try to use section in drawer. It's this case, I can't use android:checkableBehavior="single" and I lost the color change in the selection of an item.
<item
android:id="#+id/section"
android:title="#string/section_title">
<menu>
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
<item
android:id="#+id/drawer_downloaded"
android:icon="#drawable/ic_file_download_black_24dp"
android:title="#string/downloaded"/>
</menu>
</item>
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/home"/>
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
...
<item
android:id="#+id/drawer_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/settings"/>
<item
android:id="#+id/section"
android:title="#string/section_title">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
<item
android:id="#+id/drawer_downloaded"
android:icon="#drawable/ic_file_download_black_24dp"
android:title="#string/downloaded"/>
</group>
</menu>
</item>
</group>
</menu>
you can check this solution for details..
I am unable to set a submenu item as checked
As a workaround until the bug reported by Fondesa is fixed you can use this:
Menu Definition
<item android:checkable="true" ...>
Styling
<item android:state_selected="true" android:color="#color/error_color"/>
This will properly highly the menu item when selected.
Note that this will not address the requirement:
android:checkableBehavior="single"
You will have to handle that manually.

Categories

Resources