Is it possible to change material design icon color from xml in Android?

前端 未结 9 1056
囚心锁ツ
囚心锁ツ 2020-12-05 06:39

I\'m actually trying to use colored icons in my app. I\'ve downloaded the official material design icon pack from here. Now all the icons in this pack are either white, grey

相关标签:
9条回答
  • 2020-12-05 07:25

    Many alternatives here already in this thread. Perhaps I can add one more for whoever finds it convenient :

    You can also use the Drawable class, the code being as follows

        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.ic_play_circle_filled);
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, getResources().getColor(R.color.colorPrimary));
    

    Although the above did the trick for me, but for my use case suggicient was to use android:tint

    0 讨论(0)
  • 2020-12-05 07:32

    How to change material icon color in XML

    <ImageButton 
         android:layout_width="YourValue"
         android:layout_height="YourValue"
         ...
         android:tint="YourColor" // this line do the magic
    />
    
    0 讨论(0)
  • 2020-12-05 07:39

    I was looking for the same thing but to change it dynamically in the code. I hope this helps someone looking for the same thing.

    Create an ImageView for the icon and use setColorFilter on that view.

    ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
    imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));
    
    0 讨论(0)
提交回复
热议问题