Brijesh Kumar Vaishya
Brijesh Kumar Vaishya

Reputation: 11

How to change color of svg image in android. Images are downloaded from web api

I have an issue something like, I am downloading a image from web api and successfully set into imageview. I am unable to change the color of svg image that I downloaded. I tried tintColor in xml and setFilterColor() from .java file but nothing is work for me. Is there any solution please reply on this post.

public static void loadSvgWithColor(final Context context, String url, final int color, final ImageView target) {
    if (httpClient == null) {
        httpClient = new OkHttpClient.Builder()
                .cache(new Cache(context.getCacheDir(), 5 * 1024 * 1014))
                .build();
    }
    if (!url.equals("")) {
        Request request = new Request.Builder().url(url).build();
        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                target.setImageResource(R.drawable.app_icon);
                target.setColorFilter(color);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream stream = response.body().byteStream();
                Sharp.loadInputStream(stream).into(target);
                stream.close();
                target.setColorFilter(color);
       }
        });
    } else {
        target.setImageResource(R.drawable.app_icon);
        target.setColorFilter(color);
    }
}

Upvotes: 0

Views: 2115

Answers (2)

Paul LeBeau
Paul LeBeau

Reputation: 101820

The Sharp library you are using generates a PictureDrawable and puts that in your ImageView. Last I checked, the android:tint and setColorFilter() methods don't work on PictureDrawables.

I can think of a couple of solutions:

Upvotes: 0

Razor
Razor

Reputation: 457

Edit the stream of svg file when you received of api, and set into imageview By this sample: without Edit color

Edited color

Upvotes: 1

Related Questions