Example usage for android.content Context getColorStateList

List of usage examples for android.content Context getColorStateList

Introduction

In this page you can find the example usage for android.content Context getColorStateList.

Prototype

@NonNull
public final ColorStateList getColorStateList(@ColorRes int id) 

Source Link

Document

Returns a color state list associated with a particular resource ID and styled for the current theme.

Usage

From source file:android.support.v7.content.res.AppCompatResources.java

/**
 * Returns the {@link ColorStateList} from the given resource. The resource can include
 * themeable attributes, regardless of API level.
 *
 * @param context content to inflate against
 * @param resId the resource identifier of the ColorStateList to retrieve
 *///from   w w  w  . j  a va2s  .  c o  m
public static ColorStateList getColorStateList(@NonNull Context context, @ColorRes int resId) {
    if (Build.VERSION.SDK_INT >= 23) {
        // On M+ we can use the framework
        return context.getColorStateList(resId);
    }

    // Before that, we'll try handle it ourselves
    ColorStateList csl = getCachedColorStateList(context, resId);
    if (csl != null) {
        return csl;
    }
    // Cache miss, so try and inflate it ourselves
    csl = inflateColorStateList(context, resId);
    if (csl != null) {
        // If we inflated it, add it to the cache and return
        addColorStateListToCache(context, resId, csl);
        return csl;
    }

    // If we reach here then we couldn't inflate it, so let the framework handle it
    return ContextCompat.getColorStateList(context, resId);
}