0

I know there are many topics discussing tint functionality prior to sdk 21, but they either use some kind of imageviews (which I dont) or solve it programmatically (which I dont want to).

My case:

I have a constraint layout with a 9-patch drawable as background:

<android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/testDrawable">

This is my drawable:

<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ninepatchdrawable"
android:tint="@color/lightBlue"
android:tintMode="multiply" />

This works fine for updated devices, but as I said the tint isn't working for devices sdk < 21. I don't get any kind of error message either.

I already tried changing my layout to:

<android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/testDrawable"
        android:backgroundTint="@color/lightBlue"
        android:backgroundTintMode="multiply">

or using app:backgroundTint as suggestet in another question.

Thanks in advance

2 Answers2

2

You can try something like this:

ImageView.getDrawable().setColorFilter(new PorterDuffColorFilter(context.getResources().getColor(**color**), PorterDuff.Mode.SRC_IN));

Don't believe it'll work with a lower API -> but its a good way to change tint on the fly

letsCode
  • 2,774
  • 1
  • 13
  • 37
2

There is an obvious reason why.

If you check the docs. The first part of the first line is:

With Android 5.0 (API level 21) and above,

Because android:tint was added with material design in API 21. And because it was added in API 21, it is a tag that has no meaning on API 20 and lower.

Using the support library (appcompat library) should allow you to use it. You need to add another namespace, but that isn't the biggest job to do. You can also do it in styles.xml

And backgroundTint is something completely different as you can see in this question

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Jeah, I already knew that its introduced in api level 21, thats why I know my problem is below that. I haven't found out yet how to use the support library to set the tint in xml directly, can you give me a hint on that? – Programmer879 Jul 31 '17 at 16:18
  • First off, you need this Gradle dependency: `compile 'com.android.support:appcompat-v7:25.3.1'`. If you are not targeting API 25, you need a different version. Secondly, I recommend you use styles.xml for this part, you create a new style and add whatever you want. then add this line: `#FFF` where you replace #FFF with a @color resource or the color you want. Adding `android:` in front of `tint` means you use the ANdroid resource and not the appcompat resource – Zoe Jul 31 '17 at 16:23
  • I did everything as you said and I get `Error:(1714, 21) No resource found that matches the given name: attr 'tint'`. My target api is 24 and i use the following support library version: `compile 'com.android.support:appcompat-v7:24.2.1'` – Programmer879 Jul 31 '17 at 16:39
  • Correction: (my bad) you need an AppCompatImageView and use `android:tint`. It should work with nine patches – Zoe Jul 31 '17 at 16:49
  • Not from the looks of things as that is something different. I guess I have an idea, but I am not sure if it works as I cannot test it. Keep the drawable file. Add a FrameLayout as the root layout. Inside the framelayout and outside the constaintlayout you have the APpCompatImageView. There you have the tint and all of that. **READ:** ADD FrameLayout. Do not delete the ConstraintLayout. Add a FrameLayout as the root element and put the COnstraintLayout inside it and add the AppCompatImageView inside teh framelayout. It is usable as a background now, and you can add stuff over it – Zoe Jul 31 '17 at 17:02
  • I've experimented with this, and I cant manage to get my layout placed only inside the conent area of my 9 patch. The layout is spread around the scaled parts too. – Programmer879 Aug 01 '17 at 07:17