Setting the View MenuItem view mode programmatically
Is it possible to programmatically set the style of the MenuItem in the new navigation mode? I am building the menu dynamically and cannot use a static XML file. I could not find information on this.
UPDATE: I am familiar with customizing the title and icon, but not in the way I can for example. set the alpha of the text.
UPDATE 2: The NavigationView class has a setItemTextColor(ColorStateList)
. As far as I can tell, it is impossible to set individual colors of objects? Thanks to
source to share
If you want to set a menu icon:
Install first navigationView.setItemIconTintList(null)
,
NavigationView navigationView = (NavigationView) findViewById(R.id.main_nav_view);
navigationView.setItemIconTintList(null);
and get a menu item, set an icon to it.
MenuItem menuItem = navigationView.getMenu().getItem(0);
menuItem.setIcon(R.drawable.nav_new_drawable);
And also you can set the color of the text, for example:
navigationView.setItemTextColor(ColorStateList textColor);
And set the background of the element like:
navigationView.setItemBackgroundResource(int resId)
--- Edit ---
Since you already know the above, you can try the code below.
<color name="test_alpha_color">#40999999</color>
MenuItem menuItem = navigationView.getMenu().getItem(0);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.test_alpha_color)), 0, s.length(), 0);
menuItem.setTitle(s);
source to share