0

I have this xml code :

<item android:id="@+id/search_id" android:title="Search" app:showAsAction="always"> <bitmap android:src="@drawable/ic_search_black_24dp" android:tint="@color/colorAccent"> </bitmap> </item> <item android:id="@+id/shoppingcart_id" android:title="Shopping Cart" android:icon="@drawable/ic_shopping_cart_black_24dp" app:showAsAction="always"></item> 

they both are black icons but the first icon I want to change into red. but when I run my app, it doesn't show anything. Is it possible to change PNG file color from drawable resource just using XML without adding code in java?

thank you.

1
  • Possible duplicate of Tint menu icons Commented Feb 17, 2017 at 14:59

1 Answer 1

1

Edit

Woops, just realized you were talking about a menu icon, so, just try wrapping your bitmap in a drawable file, like explained in here: https://stackoverflow.com/a/39535399/7296930


The solution depends on your API version.

For API 21+:

Just try defining the icon as another drawable with a tint, and reference this new drawable as the icon (instead of the < bitmap > tag) ?

For API<21:

Juste follow the solution here, using a custom TintableImageView and custom attribute. You will have to code some Java to define the TintableImageView widget, but then your drawable will be defined only in xml, with something like that:

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <com.example.widgets.TintableImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/example" app:tint="@color/color_selector"/> </LinearLayout> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.