How to change drawer layout icon colours - android

I'm updating an existing project from a different developer in our company and am trying to set the colour of the icons to match the text (as you can see from the screenshot below). I've succeeded for the current selected item but not for the items that aren't selected. I cannot figure out why either of these do/don't work at the moment.
My layout
<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">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/appbar_basic"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include layout="#layout/activity_home_content"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<include layout="#layout/navigation_view"/>
</android.support.v4.widget.DrawerLayout>
My code
#Bind(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
private void initDrawer() {
Menu m = mNavigationView.getMenu();
for (ModuleVO module : Modules.getActiveModules()) {
m.add(0, module.id, 1, module.textRef).setIcon(module.drawerIconRef);
}
}
I have already done some searching and tried a couple of things, including the answers listed here:
Changing text color of menu item in navigation drawer
How to style Menu Items in Navigation Drawer in Android?
Using app:itemIconTint doesn't work and neither does writing a selector. The only way I've been able to change the colours has been by changing these attributes in my themes.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">#color/text</item>
<item name="android:textColorSecondary">#color/primary</item>
</style>
</resources>
I will be happy to provide more information/code if required to solve the issue!

I have also have that issue but I have found that solution may be it will work for you :-
<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_home"
app:itemBackground="#android:color/transparent"
app:itemIconTint="#drawable/drawer_item"
app:itemTextColor="#drawable/drawer_item"
app:menu="#menu/activity_home_drawer" />
and drawer_item.xml:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorPrimaryDark" android:state_checked="true" />
<item android:color="#000000" />
</selector>
nav_header_home.xml is:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#000"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#drawable/user"
tools:ignore="VectorDrawableCompat" />
<LinearLayout
android:id="#+id/llLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tvSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:text="Sign In"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#fff" />
<TextView
android:id="#+id/tvJoin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:text="Join Free"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

I found the answer! When looking over my layout file again I found <include layout="#layout/navigation_view"/> and when I applied a selector to navigation_view it worked! Below is my working code.
activity_home.xml
<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">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/appbar_basic"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include layout="#layout/activity_home_content"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<include layout="#layout/navigation_view"/>
</android.support.v4.widget.DrawerLayout>
navigation_view.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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:itemIconTint="#drawable/drawer_item"
app:itemTextColor="#drawable/drawer_item"
/>
</merge>
drawer_item.xml (Kudos to Abhinav Gupta for this part)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_checked="true" />
<item android:color="#color/text" />
</selector>

Related

How to center a custom layout in navigation drawer?

I created a navigation drawer and listed menu icons. After that I had to include a custom layout bottom of the menu list.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:layout_gravity="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
ndroid:layout_height="?attr/actionBarSize"
app:titleTextColor="#color/white"
android:background="?attr/colorPrimary"
ndroid:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</FrameLayout>
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<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:menu="#menu/drawer_view_menu"
app:headerLayout="#layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>
drawer_view_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_prof"
android:title="#string/my_profile" />
........other items here.......
</group>
<group android:checkableBehavior="single">
<item app:actionLayout="#layout/toggle_button"
android:title="#string/blank"
/>
</group>
toggle_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/left_rounded"
android:backgroundTint="#color/red"
android:text="#string/lang_en"
android:textColor="#color/white"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/right_rounded"
android:text="#string/lang_ar" />
Then it displayed the custom layout but it is aligned right side. I need this to be aligned center horizontally. Also attached a problem illustration below.
Problem illustration
After some research on the internet I came up with a solution.
Steps
Included my custom layout(toggle_button.xml) in NavigationView Layout.
NavigationView Layout
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:layout_gravity="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
ndroid:layout_height="?attr/actionBarSize"
app:titleTextColor="#color/white"
android:background="?attr/colorPrimary"
ndroid:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</FrameLayout>
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<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:menu="#menu/drawer_view_menu"
app:headerLayout="#layout/nav_header">
<include layout="toggle_button.xml""/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
2.Updated my custom layout for making toggle aligned center in bottom.
toggle_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="#dimen/margin_medium"
android:layout_gravity="bottom">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/left_rounded"
android:backgroundTint="#color/red"
android:text="#string/lang_en"
android:textColor="#color/white"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/right_rounded"
android:text="#string/lang_ar" />
</LinearLayout>
Also removed the second group from drawer_view_menu.xml
Done! It was as simple as that....

Drawer items not taking whole width

I have a menu drawer with several items, and for some reason the items don't take the whole width of the drawer, even though layout_width is match parent.
Here's the drawer_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/drawer_user_current"
android:title=""
app:actionLayout="#layout/nav_drawer_current_user_profile" />
</menu>
Here is nav_drawer_current_user_profile.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/img_chat_room_icon"
android:layout_width="#dimen/size_large"
android:layout_height="#dimen/size_large"
android:src="#drawable/ic_person_avatar_blue_24dp" />
<TextView
android:id="#+id/txt_user_profile_nav_drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Current user"
android:textColor="#color/light_blue"
android:textSize="#dimen/nav_drawer_font_size" />
</LinearLayout>
The drawer itself is in this file base_layout.xml
<android.support.v4.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"
tools:openDrawer="start">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu_chat"/>
And this is how the menu is rendered
How can I make the item to be in the right, and to take all the width of the drawer?
try
android:title="#null"
in menu item
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
app:menu="#menu/drawer_menu_chat"/>

Scrollable footer in Drawer

I tried to add a footer to a drawer. It works but it remain fixed and it cover some menu item. How can I do to have the footer at the end of the list (after the last element). It takes the menu item from a xml (activity_main_drawer.xml) and it has a header (nav_header_main.xml)
The code of the drawer is this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/side_nav_bar"
android:id="#+id/content_main"/>
</LinearLayout>
<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"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer">
<include layout="#layout/nav_footer_main"></include>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
the code of footer (nav_footer_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="#dimen/nav_footer_height"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#drawable/side_nav_bar"
android:baselineAligned="false"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:scaleType="centerCrop"
app:srcCompat="#mipmap/copyright"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/copyright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/copyright" />
</LinearLayout>
According to #Adreamus answer to the question How to add footer to NavigationView - Android support design library?
you could make it by 2 navigation views, look at this app example on Github
Also, I suggest you use MaterialDrawer Library it will save you a lot of time
You can add footer as an item in the menu of the navigation view like this
menu_main:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:title="#string/home" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_about_me"
android:title="#string/about" />
</group>
<group>
<item
android:id="#+id/nav_footer"
android:title=""
app:actionLayout="#layout/nav_footer_main"
app:showAsAction="never" />
</group>
</menu>
It will be scrollable and also easy to access from the code, you can use this:
val navigationFooter = navView.menu.findItem(R.id.nav_footer).actionView as View
navigationFooter.findViewById<View>(R.id.btn_call_us).setOnClickListener {
onCallButtonClicked()
}
Good luck!

set navaigation Item text color white android

I want to set navigation menu text color to white how can able do it??
I want to make screen like this
Please check my following code snippet
My Navigation Menu Looks Like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/nav_timer"
android:icon="#drawable/ic_av_timer_black_36dp"
android:title="Timer"
android:textColor="#000000"
/>
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_account_circle_black_48dp"
android:title="Profile"
>#color/menuColor</item>
<item
android:id="#+id/nav_logs"
android:icon="#drawable/ic_assignment_black_48dp"
android:title="Logs" />
<item
android:id="#+id/nav_changeBusiness"
android:icon="#drawable/ic_business_black_48dp"
android:title="Change Business" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_48dp"
android:title="Logout" />
Add app:itemTextColor="#android:color/white" in your navigation 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"
app:itemTextColor="#android:color/white"
android:fitsSystemWindows="true"
app:menu="#menu/act_home_drawer_menu"
/>
It will give you desired result.
You can add a Listview under Navihation View. And then you can add your custom adapter with textview.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/content_view"
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>
This will solve your problem. It worked for me.

Android right to left NavigationDrawer menu items aren't RTL

I need to align navigation menu items to the right.
I read many articles and questions and answers but i couldn't find what's wrong in my project.
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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="right">
<include
layout="#layout/content_activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_activity_home"
app:menu="#menu/activity_home_drawer" />
This is activity_home_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/menu_account"
android:icon="#drawable/ic_profile"
android:title="حساب کاربری" />
<item
android:id="#+id/menu_logout"
android:icon="#drawable/ic_sign_out"
android:title="خروج از حساب" />
</group>
I can make drawer itself to open from right but elements of NavigationView still remain left to right. You can see the result here:
As you see menu items aren't right to left. How to make them rtl?
try adding android:layoutDirection="rtl"
<android.support.design.widget.NavigationView
android:layoutDirection="rtl"
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
This works for api level 17 or above. For older devices there is a trick.
In application level of android manifest set supportRtl="false" and in layouts set layout_gravity="right". This works properly.
First delete app:menu="#menu/activity_main_drawer" and in
"#layout/nav_header_main" layout you can set custom layout.in the below you can see content of nav_header_main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="right"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/banner"
android:gravity="right">
</LinearLayout>
</LinearLayout>

Categories

Resources