I'm trying to add a night theme for my application, and I spent almost three hours trying to make the text and icons in my navigation box white with a dark background. This is how I try to do this in onCreate() in MainActivity.java :
navigationView = (NavigationView) findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { // This method will trigger onItemClick of navigation menu @Override public boolean onNavigationItemSelected(MenuItem menuItem) { // Checking if the item is in checked state or not, if not make it in checked state if (menuItem.isChecked()) menuItem.setChecked(false); else menuItem.setChecked(true); if (nightMode == 0) { SpannableString spanString = new SpannableString(menuItem.getTitle().toString()); spanString.setSpan(new ForegroundColorSpan(Color.WHITE), 0, spanString.length(), 0); // fix the color to white menuItem.setTitle(spanString); }
The nightMode value of nightMode does not matter because it works. When night mode is on (0), any menu item selected in the navigation box turns white. However, this only happens when each item is selected, which is clearly inconvenient. Here is my drawer_dark.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/unitone" android:checked="true" android:icon="@drawable/one_white" android:title="Classical Period" /> <item android:id="@+id/unittwo" android:checked="false" android:icon="@drawable/two_white" android:title="Postclassical Period" /> <item android:id="@+id/unitthree" android:checked="false" android:icon="@drawable/three_white" android:title="Early Modern Era" /> <item android:id="@+id/unitfour" android:checked="false" android:icon="@drawable/four_white" android:title="Dawn of Industrial Age" /> <item android:id="@+id/unitfive" android:checked="false" android:icon="@drawable/five_white" android:title="Modern Era" /> </group> </menu>
I use white icons on a transparent background for each item, but they appear as black on the black background of the navigation drawer. I tried looking for an xml solution to change the color of the text, and I scratch my head because I donβt know why this was missed.
Can someone suggest me a dynamic solution to get what I'm trying to achieve? Thanks everyone, thanks!
EDIT: I do not use a third-party library, this is the NavigationView provided in the support library. Here's the XML layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:elevation="7dp" tools:context=".MainActivity" android:fitsSystemWindows="true" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/ColorDark" /> <include layout="@layout/toolbar" /> </FrameLayout> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:background="#000" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_gravity="start" app:headerLayout="@layout/header" app:menu="@menu/drawer" /> </android.support.v4.widget.DrawerLayout>
android xml menu
wasimsandhu Aug 17 '15 at 4:28 2015-08-17 04:28
source share