javaandroidtint

How to set and get tint color on SVG programmaticaly in android?


I have an ImageView which has an SVG as source example: android:src="@drawable/bold_svg".

Now on click I want to set the tint color to color accent or return it to white. Two states.

What I have tried:

    myImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int currentColor, colorAccent;
            currentColor=ImageViewCompat.getImageTintList(myImageView).getDefaultColor();
            colorAccent=getResources().getColor(R.color.colorAccent);
            if (currentColor==colorAccent) {
                myImageView.setColorFilter(getResources().getColor(R.color.white_text_color));
            } else {
                myImageView.setColorFilter(getResources().getColor(R.color.colorAccent));
            }
        }
    });

It looks like every time I click the button, currentColor it does not change, so else is the only thing that is called! What am I missing?


Solution

  • I managed to find the answer something like this would work:

        myImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentColor, colorAccent;
                currentColor=ImageViewCompat.getImageTintList(myImageView).getDefaultColor();
                colorAccent=getResources().getColor(R.color.colorAccent);
                if (currentColor==colorAccent) {
                    ImageViewCompat.setImageTintList(ivBold, ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white_text_color)));
                } else {
                    ImageViewCompat.setImageTintList(myImageView, ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)));
                }
            }
        });