Search code examples
androidcolorsimageviewpicassotint

How to change color of a imageview that I load from Picasso from Green to Red


This is my code:

backPic.setScaleType(ImageView.ScaleType.FIT_CENTER);
backPic.setColorFilter(ContextCompat.getColor(context, R.color.red), android.graphics.PorterDuff.Mode.MULTIPLY);
Picasso.with(context).load(icon).into(backPic, new com.squareup.picasso.Callback() {
    @Override
    public void onSuccess() {
    }

    @Override
    public void onError() {
        UserVehicle.setVehicleClassPic(getVclass().getId(), backPic);
    }
});

The initial picture is this:

enter image description here

This is what I get now:

enter image description here

How can I make it so the bike in the second picture is red, not gray towards green?

The color I sent to the image filter is red.


Solution

  • Using the comment from André Sousa I did this code:

       Picasso.with(context).load(icon).into(backPic, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        DrawableCompat.setTint(backPic.getDrawable(), context.getResources().getColor(R.color.red));
                    }
    
                    @Override
                    public void onError() {
                        UserVehicle.setVehicleClassPic(getVclass().getId(), backPic);
                    }
                });
    

    So it would set the tint after the Picasso image has loaded, which works