The color of the primary darkness for the gradient

Recently, I wanted to make my color gradient status. I know how WindowManager works. But I decided instead to find another way to color my status bar with a gradient.

So I did it,

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

@ extensible / 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>

Gradient @ drawable / gradient is the color of the gradient that I configured. Although the IDE stated that this is not the right way to do this, but it works.

My question is: Is this correct? Does anyone have this experience?

+4
source share
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...

, SO , , , / . , , Android , - , @color. , " " .

, .

SO, , API 24+, , , API 25 26. - Android , ..

+2

, , 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);
    }
} 

style.xml.

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

activity AndroidManifest.xml

<activity android:name=".ui.MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
</activity>
0

Source: https://habr.com/ru/post/1665854/


All Articles