Change drawable color programmatically android

Change fillColor of a vector in android programmatically

I want to edit the fill Color of a vector-file in Android programmatically.

In the xml-file I can set my color with the attribute android:fillColor but I want to change the color in runtime.

Any examples for that? Thanks.

7 Answers 7

This is exactly what you need. Credits to @emmaguy, the author of the post. I just added the full support of Support Library 23.4+, which enables you to stop generating pngs at runtime:

And if this line is set on your Activity’s or Application’s onCreate:

You can use your SVGs not only with srcCompat but also with other attributes such as drawableLeft , background , etc. in TextView, ToggleButton and so on. It also works if used on selectors.

Note: I modified the code to use VectorDrawableCompat.create instead of ResourcesCompat.getDrawable . Otherwise it would not work and throw org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector .

Medium post content:

First, we create attributes for the two kinds of bauble, so we can change their colours:

Then, in the VectorDrawable, set the parts we want to dynamically change to use these attributes:

Create themes and set the colours you want to use:

Use the drawable in an ImageView:

That’s it! When you want to change the colours, simply set a different theme and your drawable will update. See the GitHub repo for a full sample.

Источник

Change drawable color programmatically

I’m trying to change the color on a white marker image by code. I have read that the code below should change the color, but my marker remains white.

Читайте также:  Android app access manager

Did I miss something? Is there any other way to change colors on my drawables located in my res folder?

19 Answers 19

Using DrawableCompat is important because it provides backwards compatibility and bug fixes on API 22 devices and earlier.

You can try this for svg vector drawable

You may need to call mutate() on the drawable or else all icons are affected.

You can try this for ImageView . using setColorFilter() .

Another way to do this on Lollipop, android 5.+ is setting a tint on a bitmap drawable like such:

This will work for you if you have a limited number of colors you want to use on your drawables. Check out my blog post for more information.

I have wrote a generic function in which you can pass context, icon is id drawable/mipmap image icon and new color which you need for that icon.

This function returns a drawable.

You could try a ColorMatrixColorFilter, since your key color is white:

This worked for me. Make sure to put «ff» between 0x and color code. Like this 0xff2196F3

Same as the accepted answer but a simpler convenience method:

Note, the code here is Kotlin.

You may want to try Mode.LIGHTEN or Mode.DARKEN . The Android Javadocs are horrible at explaining what the PorterDuff Modes do. You can take a look at them here: PorterDuff | Android

I suggest looking around at Compositing on Mozilla’s site here. (They don’t have all the modes that android does but they have a lot of them)

Use this: For java

you can use PorterDuff.Mode.SRC_ATOP, if your background has rounded corners etc.

For those who use Kotlin a simple extension function:

Источник

Android & Kotlin Examples code

Android kotlin tutorials and examples code for android apps developers.

Android change vector drawable color programmatically

In the xml-file you can set custom color with the attribute android:fillColor but to change the color of vector drawable in runtime/dynamically. Tere is need to Change fillColor of a vector drawable in android programmatically, you can edit fill Color of a vector-file in Android programmatically using DrawableCompat.setTint() method which change tint color of whole vector drawable.

Читайте также:  Android studio что такое api

Here, ImageView is set with drawable vector icon of black color. This example changes android ImageView srcCompat vector drawable tint color to colorAccent as defined in the colors.xml.

xmlns:app=»http://schemas.android.com/apk/res-auto»
xmlns:tools=»http://schemas.android.com/tools»
android:layout_width=»match_parent»
android:layout_height=»match_parent»
android:orientation=»vertical»
android:gravity=»center_horizontal»
tools:context=».MainActivity»>

package com.example.espl.changedrawablecolor;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity <

Источник

How to change drawable tint of a button below api level 23 programmatically in android

I am trying to figure out how to change the color of drawableLeft/drawableRight of button programmatically . I have used drawable tint in my xml as mentioned below which works > api level 23 but not able to change the color Follow

5 Answers 5

Here is a quick way to tint your TextView or Button drawable:

If you prefer to use XML code instead of JAVA you can change the android:drawableTint attribute to app:drawableTint .

For kotlin this works for me

Or if you use a ressource

you are using PorterDuff.Mode.MULTIPLY , so you are multiplying colors. assuming (name of your drawable) your icon is black — #000000 or as int it will be 0 . then 0 * GRAY (or any other color) will always give you 0 , so still black.

try other PorterDuff.Mode s, e.g. PorterDuff.Mode.SRC_ATOP or PorterDuff.Mode.SRC_IN

your current code will probably work with white version of icon, which should be colored properly with MULTIPLY

it will make the button or any view 50% tint

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Android как стать человеком

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to change color of vector drawable path on button click

With the new android support update, vector drawables get backward compatibility. I have a vector image with various paths. I want the color of the paths to change on click of a button or programmatically based on an input value. Is it possible to access the name parameter of the vector path? And then change the color.

8 Answers 8

The color of the whole vector can be changed using setTint.

You have to set up your ImageView in your layout file as this:

Then to change the color of your image:

Note: myImageView.getDrawable() gives nullpointerexception if the vector drawable is set to the imageView as background.

Use this to change a path color in your vector drawable

There are several ways of doing the same stuff, but this works for both Vector Drawables as well as SVG (Local/Network).

(change R.color.headPink with color of your choice)

You can use this method to change color in lower API to change vector color in fragment

in place of getActivity you should use MainActivity.this for changing vector color in activity

You can change the color of individual path at runtime, without using reflection.

VectorMaster introduces dynamic control over vector drawables. Each and every aspect of a vector drawable can be controlled dynamically (via Java instances), using this library.

Just add the following dependency in your app’s build.gradle

In your case you need a simple color change:

Vector example: your_vector.xml

XML:

Java:

You have now full control over vector drawables.

Источник

Оцените статью