Java Code Examples for androidx.appcompat.content.res.AppCompatResources#getColorStateList()

The following examples show how to use androidx.appcompat.content.res.AppCompatResources#getColorStateList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MaterialResources.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link ColorStateList} from the given {@link TypedArray} attributes. The resource
 * can include themeable attributes, regardless of API level.
 */
@Nullable
public static ColorStateList getColorStateList(
    @NonNull Context context, @NonNull TypedArray attributes, @StyleableRes int index) {
  if (attributes.hasValue(index)) {
    int resourceId = attributes.getResourceId(index, 0);
    if (resourceId != 0) {
      ColorStateList value = AppCompatResources.getColorStateList(context, resourceId);
      if (value != null) {
        return value;
      }
    }
  }

  // Reading a single color with getColorStateList() on API 15 and below doesn't always correctly
  // read the value. Instead we'll first try to read the color directly here.
  if (VERSION.SDK_INT <= VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    int color = attributes.getColor(index, -1);
    if (color != -1) {
      return ColorStateList.valueOf(color);
    }
  }

  return attributes.getColorStateList(index);
}
 
Example 2
Source File: MaterialResources.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link ColorStateList} from the given {@link TintTypedArray} attributes. The
 * resource can include themeable attributes, regardless of API level.
 */
@Nullable
public static ColorStateList getColorStateList(
    @NonNull Context context, @NonNull TintTypedArray attributes, @StyleableRes int index) {
  if (attributes.hasValue(index)) {
    int resourceId = attributes.getResourceId(index, 0);
    if (resourceId != 0) {
      ColorStateList value = AppCompatResources.getColorStateList(context, resourceId);
      if (value != null) {
        return value;
      }
    }
  }

  // Reading a single color with getColorStateList() on API 15 and below doesn't always correctly
  // read the value. Instead we'll first try to read the color directly here.
  if (VERSION.SDK_INT <= VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    int color = attributes.getColor(index, -1);
    if (color != -1) {
      return ColorStateList.valueOf(color);
    }
  }

  return attributes.getColorStateList(index);
}
 
Example 3
Source File: FABSpeedDial.java    From Kore with Apache License 2.0 6 votes vote down vote up
private void setupFABIcon(Context context) {
    TypedValue tv = new TypedValue();

    context.getTheme().resolveAttribute(R.attr.iconFABDialsOpenClose, tv, false);
    iconFABOpenClose = AppCompatResources.getDrawable(context, tv.data);
    context.getTheme().resolveAttribute(R.attr.iconFABDefault, tv, false);
    iconFABDefault = AppCompatResources.getDrawable(context, tv.data);

    FABMain.setImageDrawable(dialsEnabled ? iconFABOpenClose : iconFABDefault);

    ColorStateList colorStateList = AppCompatResources.getColorStateList(context, R.color.fabspeeddial);
    int fabColorNormal = colorStateList.getColorForState(new int[] {android.R.attr.state_enabled},
                                                         R.attr.colorPrimaryDark);
    int fabColorPressed = colorStateList.getColorForState(new int[] {android.R.attr.state_pressed},
                                                          R.attr.colorPrimary);

    busyAnimation = new PulsateAnimation(FABMain, fabColorNormal, fabColorPressed);

    FABMain.setBackgroundTintList(colorStateList);
}
 
Example 4
Source File: Utils.java    From AndroidFastScroll with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ColorStateList getColorStateListFromAttrRes(@AttrRes int attrRes,
                                                          @NonNull Context context) {
    TypedArray a = context.obtainStyledAttributes(new int[] { attrRes });
    int resId;
    try {
        resId = a.getResourceId(0, 0);
        if (resId != 0) {
            return AppCompatResources.getColorStateList(context, resId);
        }
        return a.getColorStateList(0);
    } finally {
        a.recycle();
    }
}
 
Example 5
Source File: StyledAttributesHelper.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
public ColorStateList getColorStateList(int attr) {
    int index = getAttributeIndex(attr);
    int res = mArray.getResourceId(index, 0);
    if (res != 0) {
        ColorStateList v = AppCompatResources.getColorStateList(mContext, res);
        if (v != null)
            return v;
    }
    return mArray.getColorStateList(index);
}
 
Example 6
Source File: Utils.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ColorStateList getColorStateList(Context context, TypedArray original, int index) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return original.getColorStateList(index);
    }

    int resId = original.getResourceId(index, 0);
    return AppCompatResources.getColorStateList(context, resId);
}
 
Example 7
Source File: ThemePreferencesManager.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("RestrictTo")
public void showChooseThemePopup(View anchor) {
  PopupMenu popupMenu = new PopupMenu(context, anchor);
  popupMenu.inflate(R.menu.mtrl_choose_theme_menu);
  if (popupMenu.getMenu() instanceof MenuBuilder) {
    MenuBuilder menuBuilder = (MenuBuilder) popupMenu.getMenu();

    menuBuilder.setOptionalIconsVisible(true);

    ColorStateList defaultColor =
        AppCompatResources.getColorStateList(
            context, R.color.material_on_surface_emphasis_medium);
    int selectedColor = MaterialColors.getColor(anchor, resourceProvider.getPrimaryColor());
    int currentThemeId = getCurrentThemeId();
    for (int i = 0; i < menuBuilder.size(); i++) {
      MenuItem item = menuBuilder.getItem(i);
      if (item.getItemId() == currentThemeId) {
        DrawableCompat.setTint(item.getIcon(), selectedColor);

        SpannableString s = new SpannableString(item.getTitle());
        s.setSpan(new ForegroundColorSpan(selectedColor), 0, s.length(), 0);
        item.setTitle(s);
      } else {
        DrawableCompat.setTintList(item.getIcon(), defaultColor);
      }
    }
  }
  popupMenu.setOnMenuItemClickListener(
      item -> {
        saveAndApplyTheme(item.getItemId());
        return false;
      });
  popupMenu.show();
}