I recently wanted to make my status bar color gradient. I know how the WindowManager way works. But I decided instead to find another way to color my statusbar with gradient.
So I did this,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">@drawable/gradient</color>
<color name="colorAccent">#FF4081</color>
</resources>
@drawable/gradient
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="135" android:startColor="#f56f2c" android:endColor="#fa9f46"/>
</shape>
The @drawable/gradient is the gradient color which I set up. Although IDE said that it is not the right way to do it, but it is worked.
My question: Is it the right way to do it? Does anyone has this experience?
This will break starting from Android N (API 25) with the following error:
android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID...
It's pretty much the same error as this SO issue, and as pointed out by an answer, the error/crash is intentional. I guess the reason is because the Android gods are now being strict with not letting you use something other than color for a @color resource. So, this is definitely not the "right way" to do it.
A workaround would be to use a custom toolbar where you can use the drawable gradient for the background.
I did try this other solution on SO, which claims to work for API 24+, but alas it breaks in API 25 & 26. It does seem like something Android should simply work with but just doesn't..