Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color of Primary Dark to be Gradient

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?

like image 225
Chingiz Avatar asked Jan 05 '17 10:01

Chingiz


2 Answers

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

like image 86
Aba Avatar answered Sep 18 '22 00:09

Aba


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>
like image 24
Varun Barve Avatar answered Sep 22 '22 00:09

Varun Barve