Reputation: 683
Sorry for my english.
I need to set item text and item icon color of NavigationView dynamically, but this is not working for some reason.
It will be a bug or am I doing something wrong? XML works well, but when I do the following things, not:
My code:
navigationView = (NavigationView) findViewById(R.id.nav_view);
int[][] states = new int[][] {
new int[] { }, // default
new int[] { android.R.attr.state_focused, android.R.attr.state_pressed }, // pressed
new int[] { android.R.attr.state_selected } // selected
};
int[] colors = new int[] {
colorDefault,
colorFocused,
colorSelected
};
ColorStateList myList = new ColorStateList(states, colors);
navigationView.setItemTextColor(myList);
navigationView.setItemIconTintList(myList);
For some reason, I only get the first color :(
Upvotes: 2
Views: 1685
Reputation: 379
int[][] states = new int[][] {
new int[] { android.R.attr.state_selected } // selected
new int[] { } // default
};
int[] colors = new int[] {
colorSelected,
colorDefault
};
ColorStateList colorList = new ColorStateList(states, colors);
navigationView.setItemTextColor(colorList);
Just use only selected and the default for navigation text state. As Matei Said. You need to use in order of the most specific case.
Upvotes: 1
Reputation: 859
It's like catching exceptions: you need to start with the most specific case and then with the more general. In your case, it would be:
int[][] states = new int[][] {
new int[] { android.R.attr.state_selected } // selected
new int[] { android.R.attr.state_focused, android.R.attr.state_pressed }, // pressed
new int[] { }, // default
};
int[] colors = new int[] {
colorSelected,
colorFocused,
colorDefault
};
Does that work for you?
Upvotes: 0