0

I am trying set the icon of item menu inside NavigationView as TextDrawable. For textDrawable im using TextDrawable library, but once i set the icon for the menu item it only displays a black circle instead of generated drawable.

I did try my code to generate the TextDrawable outside the NavigationView and it displays want i need, but I can't manage to make it display as icon for my menu item.

To generate the TexDrawable:

private Drawable getLetterDrawable(String letter){
    return TextDrawable.builder()
            .beginConfig()
            .width(20)  // width in px
            .height(20) // height in px
            .endConfig()
            .buildRect(letter, Color.RED);
}

To set the icon:

        ...
        navigationView.inflateMenu(R.menu.menu_drawer_users);  
        Menu menu = navigationView.getMenu();
        menu.add("Item text here").setIcon(getLetterDrawable("A"));
        ...

And this is the unexpected result:

result of implementation

EDIT: When I set R.mipmap.ic_launcher as icon i get the android icon but also in grey color, so i can only see its border. It seems that the drawable its being "tinted" when using menuItem.setIcon().

I did try to do the following, but without success:

        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.menu_drawer, menu);
        menu.add("Test");
        menu.getItem(0).setIcon(R.mipmap.ic_launcher);
        menu.getItem(0).getIcon().clearColorFilter();
        menu.getItem(0).getIcon().invalidateSelf();

Question: How can I remove the filter and show my icon in colors?

EDIT 2 : After Andrea Basso suggestion, I did manage to remove the tint from navigationView object, but i can't still see the letter inside the square. I only see the red square but no letter inside.

The main problem remains: how to put TextDrawable as icon on MenuItem?

Bugdr0id
  • 2,962
  • 6
  • 35
  • 59

1 Answers1

1

NavigationView has the method setItemIconTintList (ColorStateList tint), which is what you're looking for.

A ColorStateList is an XML file that defines the different filters for different states. Here's the documentation page.

Andrea
  • 351
  • 1
  • 7
  • this solves the problem with tint, but i still have the main problem: how to make TextDrawable visible as icon. I can only see the red square now, no letter inside. – Bugdr0id Oct 21 '15 at 12:53
  • @Bugdr0id I don't really have experience with that library, are you sure it's not a bug of TextDrawable? I don't know, but you may try to contact the library dev. – Andrea Oct 21 '15 at 12:59