How to change the text and icon color of selected menu item on Navigation Drawer programmatically using java

前端 未结 5 985
栀梦
栀梦 2020-12-14 01:44

I am a beginner in Android Development. Using android\'s default Navigation drawer activity I am developing an application. One of the requirements of this app is to change

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 02:48

    First Way

    try using:

    app:itemIconTint="@color/color_pink"  //selected icon color
    app:itemTextColor="@color/color_pink" //selected text color
    app:itemBackground="@color/color_gray" 
    

    For your NavigationView

    
    

    Second Way

    For programetically change use:

    navigationView.setItemTextColor(ColorStateList1);
    navigationView.setItemIconTintList(ColorStateList2);
    

    Define ColorStateList1 and ColorStateList2 as:

    For Navigation View Item Text Color

    int[][] state = new int[][] {
            new int[] {-android.R.attr.state_enabled}, // disabled
            new int[] {android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_checked}, // unchecked
            new int[] { android.R.attr.state_pressed}  // pressed
    
    };
    
    int[] color = new int[] {
            Color.WHITE,
            Color.BLUE,
            Color.WHITE,
            Color.WHITE
    };
    
    ColorStateList ColorStateList1 = new ColorStateList(state, color);
    

    For Navigation View Item Icon Color

    int[][] states = new int[][] {
            new int[] {-android.R.attr.state_enabled}, // disabled
            new int[] {android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_checked}, // unchecked
            new int[] { android.R.attr.state_pressed}  // pressed
    
    };
    
    int[] colors = new int[] {
            Color.WHITE,
            Color.BLUE,
            Color.WHITE,
            Color.WHITE
    };
    
    ColorStateList ColorStateList2 = new ColorStateList(states, colors);
    

提交回复
热议问题