There is a NavigationView in which I connect the menu c items .

Is it possible to set something like that, for example, the color of the first element would be white, and the second blue?

Those. not by clicking but statically the colors of the items were different.

NavigationView.xml :

 <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:id="@+id/navigation_view" app:menu="@menu/navigation_items" app:itemTextColor="@color/my_text_color"> 

Menu:

 <menu> <item android:id="@+id/my_id1" android:title="Элемент1"/> <item android:id="@+id/my_id2" android:title="Элемент2"/> </menu> 

    3 answers 3

    I agree with @YuriySPb absolutely, just offer a dirty and less dirty hack, this idea can be developed, here are examples:

    Example 1:

    We find NavigationView, we take out Menu then the necessary Item, and we paint in the code the text. It is possible to do this via UTF-16 by the way. I have never tried this, I am sure that it is possible, on the whole the idea is clear.

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.getMenu().getItem(1).setTitle(Html.fromHtml("<font color='#fff'>NotLikeAll</font>")); 

    enter image description here

    Example 2:

    We find NavigationView, from Menu we pull out the necessary MenuItem (second), we use the SpannableString class and the setSpan method, it waits for Object. We feed him ForegroundColorSpan and it is more loyal, because it is expanded by CharStyle, which Paint has for coloring, so the reverse code generation in UTF-16 will be on AndroidSDK consciousness, and this seems to be easier, well, a little bit ... probably ..)

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); MenuItem item_1 = navigationView.getMenu().getItem(1); SpannableString s = new SpannableString(item_1.getTitle()); s.setSpan(new ForegroundColorSpan(Color.GREEN), 0, s.length(), 0); item_1.setTitle(s); 

    enter image description here

      There is no simple way out of the box. You can only set values ​​for all items at once.

      You have to programmatically take each element in onPrepareOptionsMenu by their ID and assign it there.

      Plus try to look here . They say that you can assign your own markup for each element - so there will be even more control. But the code all one will be decent.

      • strange that he is so not thought out so much code isa small - elik
      • one
        @elik, he's just so conceived. In general, it is not supposed that someone will make points with different colors. It is, like, not quite in the design promoted by Google. - Yuriy SPb
      • @elik, if you need to absolutely cascade everything there, then it’s better to take and make your list through RecyclerView / ListView than to try to change the NavigationView - Juriy Spb
      • it is clear, thanks) anyway you will not please everyone) - elik

      I’ve been looking for a way to dynamically change the color of the text (and icons), depending on the topic. Now I use this method at once for all items. Nothing prevents to use it and in pieces. Of the benefits - immediately set any color for any state (pressed / highlighted / selected / inactive).

        int[][] states = new int[][]{ new int[]{-android.R.attr.state_checked}, // default new int[]{android.R.attr.state_checked} // checked }; int[] colors = new int[]{ AppConst.color_text_small, AppConst.color_accent }; ColorStateList color_menu_item = new ColorStateList(states, colors); navigationView.setItemTextColor(color_menu_item); navigationView.setItemIconTintList(color_menu_item);