D
D
Denis2017-05-13 19:25:59
Android
Denis, 2017-05-13 19:25:59

How to change vector color in layer-list?

Good day.
Tell me how to change the color of a vector image in a drawble layer-list resource?

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/bacground_color_project_page" />  // это цвет фона

    <item
        android:drawable="@drawable/ic_format_bold_black_48dp"  //вот это векторное изображение
        android:gravity="center"
        android:width="36dp"
        android:height="36dp"
        />
</layer-list>

I need to display this image in different colors depending on the circumstances and want to avoid storing copies of this image in different colors. I hope there is a way to change the color directly in the layer-list tag.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2017-05-15
@lacredin

I did not find how to change the color from the layer-list, but another method worked for me.
I added android:colorTint to the vector file, which referenced a drawable file with colors for different button states.
This is what the code for the vector file looks like now

<vector android:height="48dp" android:viewportHeight="24.0"
      android:viewportWidth="24.0" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android"
      android:tint="@drawable/color_state_toggle_rich_edit_text"
>
<path android:fillColor="#FF000000"
    android:pathData="M3.9,12c0,-1.71 1.39,-3.1 3.1,-3.1h4L11,7L7,7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5h4v-1.9L7,15.1c-1.71,0 -3.1,-1.39 -3.1,-3.1zM8,13h8v-2L8,11v2zM17,7h-4v1.9h4c1.71,0 3.1,1.39 3.1,3.1s-1.39,3.1 -3.1,3.1h-4L13,17h4c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5z"/>
 </vector>

This is how the color file code looks like (drawable/color_state_toggle_rich_edit_text.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_checked="true"
    android:color="@android:color/black"
    />
<item
    android:state_checked="false"
    android:color="@color/color_toggle_button"/>

I found the solution on this forum
stackoverflow.com/questions/43847170/android-how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question