6

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?

Chingiz
  • 332
  • 5
  • 17
  • Post gradient.xml also – Anshul Tyagi Jan 05 '17 at 10:06
  • You can look here: http://stackoverflow.com/questions/4381033/multi-gradient-shapes for a very thorough way of making gradients. Note that this is also dependent on the version of the min-sdk you are using. I believe gradients are not supported by sdk-1, but this is just an assumption – Roel Strolenberg Jan 05 '17 at 10:09
  • No, it is not the answer that I want. I have no problem with creating gradient. – Chingiz Jan 05 '17 at 10:22

2 Answers2

3

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..

Aba
  • 2,307
  • 2
  • 18
  • 32
2

You can do it programmatically by using the below function as stated in this SO answer.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setStatusBarGradiant(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = activity.getWindow();
        Drawable background = activity.getResources().getDrawable(R.drawable.gradient_theme);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
        window.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));
        window.setBackgroundDrawable(background);
    }
} 

You also have to add the below in your style.xml.

<style name="AppTheme.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

And then add that theme to your activity in your AndroidManifest.xml

<activity android:name=".ui.MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
</activity>
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Varun Barve
  • 323
  • 2
  • 8
  • 18