newToEverything
newToEverything

Reputation: 319

View.setBackgroundTintList before API 21?

I want to change the Backgroundtint of a View and that is the only way I found. But I get a message that it only works with certain APIs. Can I make it somehow work on up to API 15? Or is there actually a better Method to show the user a button is selected?

Upvotes: 1

Views: 2814

Answers (2)

azizbekian
azizbekian

Reputation: 62199

ViewCompat.setBackgroundTintList() will do the trick upto API 4.

Applies a tint to the background drawable.

This will always take effect when running on API v21 or newer. When running on platforms previous to API v21, it will only take effect if view implement the TintableBackgroundView interface.

Upvotes: 8

Vikas Tiwari
Vikas Tiwari

Reputation: 499

Yes,

Using java code you can change drawable color of any view this way as like:

public static void setIconColor(TextView tv, int which, int color) {
        tv.getCompoundDrawables()[which].mutate();
        tv.getCompoundDrawables()[which].setColorFilter(ContextCompat.getColor(tv.getContext(), color), PorterDuff.Mode.SRC_ATOP);
    }

    public static void setIconColor(ImageView imageView, int color) {
        imageView.getDrawable().mutate();
        imageView.getDrawable().setColorFilter(ContextCompat.getColor(imageView.getContext(), color), PorterDuff.Mode.SRC_ATOP);
    }

For User Button Selected? You should use

android:background="?attr/selectableItemBackground" 

Hope it help you !

Upvotes: 1

Related Questions