Change androidPrimary color (application panel) to gradient color

I need to change the color in the application bar to some gradient color. I created the action_bar_bg.xml file in a transferable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:centerColor="@color/colorPrimaryDark"
        android:endColor="@color/colorPrimary"
        android:startColor="@color/colorPrimary" />
</shape>

and I tried changing colorPrimary to styles.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimary">@drawable/action_bar_bg</item>

</style>

but that will not work. I get the error: android.content.res.Resources$NotFoundException: File res/drawable/action_bar_bg.xml from color state list resource ID #0x7f020046) I want to achieve this result:
app bar with gradient

What do I need to change in the code?

+4
source share
2 answers

try this way. There is a new colorPrimary attribute that you can define in your topic. This will give you an ActionBar or Toolbar a solid color.

Following a small example:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_action_bar_color</item>
</style>

: colorPrimary, android: colorPrimary, , -v21.

Android

+2

onCreate

ActionBar bar = getActionBar();
Drawable gradient = getResources().getDrawable( R.drawable.action_bar_bg);
bar.setBackgroundDrawable(gradient);
0

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


All Articles