Dynamically change SVG image color in android

29,667

Solution 1

I got where is the problem. The issue is with the color code i am using in svg file. Its not exactly 0xFF9FBF3B but #9FBF3B
But during java code you have to write it with ARGB value (e.g. 0xFF9FBF3B). I have updated it and its work fine now. I can able to change the color of svg file with same code.

Hope this will also help others to identify the actual case while changing the color of the SVG image at runtime.

Solution 2

I know it's kind of late but I also had this issue and was able to fix this issue using the setColorFilter(int color, PorterDuff.Mode mode) method.

Example:

imageView.setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);

Solution 3

Using the answer of Antlip Dev in Kotlin.

package com.example.... // Your package.

import android.graphics.PorterDuff
import android.widget.ImageView
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat

fun ImageView.setSvgColor(@ColorRes color: Int) =
    setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN)

Usage:

view.your_image.setSvgColor(R.color.gray)

Solution 4

what @Antlip Dev said is correct, but that method is deprecated now. This is the updated version:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    drawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_ATOP));
} else {
   drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}

Solution 5

You can use: drawableTint to change the color

Share:
29,667
Shreyash Mahajan
Author by

Shreyash Mahajan

About Me https://about.me/sbm_mahajan Email [email protected] [email protected] Mobile +919825056129 Skype sbm_mahajan

Updated on July 09, 2022

Comments

  • Shreyash Mahajan
    Shreyash Mahajan almost 2 years

    I know that using third party library, it is possible to use SVG image in Android. Library like: svg-android

    The code to load SVG image is like below:

     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Create a new ImageView
        ImageView imageView = new ImageView(this);
        // Set the background color to white
        imageView.setBackgroundColor(Color.WHITE);
        // Parse the SVG file from the resource
        SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
        // Get a drawable from the parsed SVG and set it as the drawable for the ImageView
        imageView.setImageDrawable(svg.createPictureDrawable());
        // Set the ImageView as the content view for the Activity
        setContentView(imageView);
    }
    

    It's working fine. I'm able to see the image. But now I want to change the color for the svg image at runtime. For that I tried the code below as mentioned in the same project description.

      // 0xFF9FBF3B is the hex code for the existing Android green, 0xFF1756c9 is the new blue color
        SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android, 0xFF9FBF3B, 0xFF1756c9);
    

    But with that I am not able to see the change in the color. So I would like to know how it is possible to change the color dynamically in Java file.