Navigation Drawer item background colour for selected item

后端 未结 13 1560
独厮守ぢ
独厮守ぢ 2020-11-28 23:54

I used Android Studio to implement the Navigation Drawer and I can\'t get the blue colour that is used to show which section we\'re currently in to change.

I\'ve tri

13条回答
  •  天涯浪人
    2020-11-29 00:17

    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()

提交回复
热议问题