Changing text color of menu item in navigation drawer

后端 未结 18 1193

I\'m trying to add a night theme for my app and I\'ve wasted nearly three hours just trying to make the text and icons in my navigation drawer turn white along with the dark

18条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 04:47

    In Future if anyone comes here using, Navigation Drawer Activity (provided by Studio in Activity Prompt window)

    The answer is -

    Use this before OnCreate() in MainActivity

    int[][] state = new int[][] {
            new int[] {android.R.attr.state_checked}, // checked
            new int[] {-android.R.attr.state_checked}
    };
    
    int[] color = new int[] {
            Color.rgb(255,46,84),
            (Color.BLACK)
    };
    
    ColorStateList csl = new ColorStateList(state, color);
    
    int[][] state2 = new int[][] {
            new int[] {android.R.attr.state_checked}, // checked
            new int[] {-android.R.attr.state_checked}
    };
    
    int[] color2 = new int[] {
            Color.rgb(255,46,84),
            (Color.GRAY)
    };
    
    ColorStateList csl2 = new ColorStateList(state2, color2);
    

    and use this in onNavigationItemSelected() in MainActivity (you dont need to Write this function if you use Navigation Drawer activity, it will be added in MainActivity).

     NavigationView nav = (NavigationView) findViewById(R.id.nav_view);
        nav.setItemTextColor(csl);
        nav.setItemIconTintList(csl2);
        nav.setItemBackgroundResource(R.color.white);
    

    Tip - add this code before If else Condition in onNavigationItemSelected()

提交回复
热议问题