Java Code Examples for androidx.core.graphics.drawable.DrawableCompat#setTintList()

The following examples show how to use androidx.core.graphics.drawable.DrawableCompat#setTintList() . 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: ChipDrawable.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Note: This should not change the size of the drawable. */
private void applyChildDrawable(@Nullable Drawable drawable) {
  if (drawable == null) {
    return;
  }
  drawable.setCallback(this);
  DrawableCompat.setLayoutDirection(drawable, DrawableCompat.getLayoutDirection(this));
  drawable.setLevel(getLevel());
  drawable.setVisible(isVisible(), false);

  if (drawable == closeIcon) {
    if (drawable.isStateful()) {
      drawable.setState(getCloseIconState());
    }
    DrawableCompat.setTintList(drawable, closeIconTint);
    return;
  }
  if (drawable.isStateful()) {
    drawable.setState(getState());
  }
  if (drawable == chipIcon && hasChipIconTint) {
    DrawableCompat.setTintList(chipIcon, chipIconTint);
  }
}
 
Example 2
Source File: RangeSeekBar.java    From RangeSeekBar with MIT License 6 votes vote down vote up
protected void applyTickMarkTint() {
    if (mTickMark != null && (mHasTickMarkTint || mHasTickMarkTintMode)) {
        mTickMark = DrawableCompat.wrap(mTickMark.mutate());

        if (mHasTickMarkTint) {
            DrawableCompat.setTintList(mTickMark, mTickMarkTintList);
        }

        if (mHasTickMarkTintMode) {
            DrawableCompat.setTintMode(mTickMark, mTickMarkTintMode);
        }

        // The drawable (or one of its children) may not have been
        // stateful before applying the tint, so let's try again.
        if (mTickMark.isStateful()) {
            mTickMark.setState(getDrawableState());
        }
    }
}
 
Example 3
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@NonNull
private Drawable createThemedBackground() {
  float cornerRadius =
      getResources().getDimension(R.dimen.mtrl_snackbar_background_corner_radius);

  GradientDrawable background = new GradientDrawable();
  background.setShape(GradientDrawable.RECTANGLE);
  background.setCornerRadius(cornerRadius);

  int backgroundColor =
      MaterialColors.layer(
          this, R.attr.colorSurface, R.attr.colorOnSurface, getBackgroundOverlayColorAlpha());
  background.setColor(backgroundColor);
  if (backgroundTint != null) {
    Drawable wrappedDrawable = DrawableCompat.wrap(background);
    DrawableCompat.setTintList(wrappedDrawable, backgroundTint);
    return wrappedDrawable;
  } else {
    return DrawableCompat.wrap(background);
  }
}
 
Example 4
Source File: BrandingUtil.java    From nextcloud-notes with GNU General Public License v3.0 6 votes vote down vote up
public static void applyBrandToEditText(@ColorInt int mainColor, @ColorInt int textColor, @NonNull EditText editText) {
    @ColorInt final int finalMainColor = getSecondaryForegroundColorDependingOnTheme(editText.getContext(), mainColor);
    DrawableCompat.setTintList(editText.getBackground(), new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_active},
                    new int[]{android.R.attr.state_activated},
                    new int[]{android.R.attr.state_focused},
                    new int[]{android.R.attr.state_pressed},
                    new int[]{}
            },
            new int[]{
                    finalMainColor,
                    finalMainColor,
                    finalMainColor,
                    finalMainColor,
                    editText.getContext().getResources().getColor(R.color.fg_default_low)
            }
    ));
}
 
Example 5
Source File: BreadcrumbsAdapter.java    From BreadcrumbsView with MIT License 6 votes vote down vote up
ArrowIconHolder(View itemView) {
	super(itemView);
	Drawable normalDrawable = getContext().getResources().getDrawable(R.drawable.ic_chevron_right_black_24dp);
	Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
	// DrawableCompat.setTint(wrapDrawable, ViewUtils.getColorFromAttr(getContext(), android.R.attr.textColorSecondary));
	DrawableCompat.setTintList(wrapDrawable, parent.getTextColor());
	imageButton = (ImageButton) itemView;
	imageButton.setImageDrawable(wrapDrawable);
	imageButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			if (item.hasMoreSelect()) {
				try {
					popupWindow.show();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	});
	createPopupWindow();
}
 
Example 6
Source File: RangeSeekBar.java    From RangeSeekBar with MIT License 6 votes vote down vote up
private void applyThumbTintInternal(final WhichThumb which) {
    Drawable thumb = which == WhichThumb.Start ? mThumbStart : mThumbEnd;

    if (thumb != null && (mHasThumbTint || mHasThumbTintMode)) {

        if (which == WhichThumb.Start) {
            mThumbStart = thumb.mutate();
            thumb = mThumbStart;
        } else {
            mThumbEnd = thumb.mutate();
            thumb = mThumbEnd;
        }

        if (mHasThumbTint) {
            DrawableCompat.setTintList(thumb, mThumbTintList);
        }

        if (mHasThumbTintMode) {
            DrawableCompat.setTintMode(thumb, mThumbTintMode);
        }

        if (thumb.isStateful()) {
            thumb.setState(getDrawableState());
        }
    }
}
 
Example 7
Source File: TextInputLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Applies a tint to the error icon drawable.
 *
 * @param errorIconTintList the tint to apply, may be null to clear tint
 * @attr ref com.google.android.material.R.styleable#TextInputLayout_errorIconTint
 */
public void setErrorIconTintList(@Nullable ColorStateList errorIconTintList) {
  this.errorIconTintList = errorIconTintList;
  Drawable icon = errorIconView.getDrawable();
  if (icon != null) {
    icon = DrawableCompat.wrap(icon).mutate();
    DrawableCompat.setTintList(icon, errorIconTintList);
  }

  if (errorIconView.getDrawable() != icon) {
    errorIconView.setImageDrawable(icon);
  }
}
 
Example 8
Source File: BottomNavigationItemView.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public void setIconTintList(ColorStateList tint) {
  iconTint = tint;
  if (itemData != null && wrappedIconDrawable != null) {
    DrawableCompat.setTintList(wrappedIconDrawable, iconTint);
    wrappedIconDrawable.invalidateSelf();
  }
}
 
Example 9
Source File: DrawableUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
public static Drawable setTintDrawable(Drawable drawable, @Nullable ColorStateList tint) {
    if (drawable != null && tint != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                DrawableCompat.setTintList(drawable, tint);
            } catch (Exception e) {
                drawable.setColorFilter(tint.getDefaultColor(), PorterDuff.Mode.SRC_ATOP);
            }
        } else {
            drawable.setColorFilter(tint.getDefaultColor(), PorterDuff.Mode.SRC_ATOP);
        }
    }
    return drawable;
}
 
Example 10
Source File: TabSwitcherStyle.java    From ChromeLikeTabSwitcher with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the icon of tabs.
 *
 * @param tab
 *         The tab, the icon should be returned for, as an instance of the class {@link Tab} or
 *         null, if the icon should not be returned for a specific tab
 * @return The icon of tabs as an instance of the class {@link Drawable} or null, if no icon is
 * set
 */
@Nullable
public final Drawable getTabIcon(@Nullable final Tab tab) {
    Drawable icon = tab != null ? tab.getIcon(model.getContext()) : null;

    if (icon == null) {
        icon = model.getTabIcon();

        if (icon == null) {
            try {
                icon = themeHelper
                        .getDrawable(tabSwitcher.getLayout(), R.attr.tabSwitcherTabIcon);
            } catch (NotFoundException e) {
                icon = null;
            }
        }
    }

    if (icon != null) {
        ColorStateList tintList = getTabIconTintList(tab);

        if (tintList != null) {
            PorterDuff.Mode tintMode = getTabIconTintMode(tab);
            DrawableCompat.setTintList(icon, tintList);
            DrawableCompat.setTintMode(icon, tintMode);
        }
    }

    return icon;
}
 
Example 11
Source File: TintHelper.java    From a with GNU General Public License v3.0 5 votes vote down vote up
@CheckResult
@Nullable
public static Drawable createTintedDrawable(@Nullable Drawable drawable, @NonNull ColorStateList sl) {
    if (drawable == null) return null;
    drawable = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTintList(drawable, sl);
    return drawable;
}
 
Example 12
Source File: ChipDrawable.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public void setCloseIconTint(@Nullable ColorStateList closeIconTint) {
  if (this.closeIconTint != closeIconTint) {
    this.closeIconTint = closeIconTint;

    if (showsCloseIcon()) {
      DrawableCompat.setTintList(closeIcon, closeIconTint);
    }

    onStateChange(getState());
  }
}
 
Example 13
Source File: IconButton.java    From materialistic with Apache License 2.0 5 votes vote down vote up
private Drawable tint(Drawable drawable) {
    if (drawable == null) {
        return null;
    }
    Drawable tintDrawable = DrawableCompat.wrap(mTinted ? drawable.mutate() : drawable);
    DrawableCompat.setTintList(tintDrawable, mColorStateList);
    return tintDrawable;
}
 
Example 14
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setBackgroundDrawable(@Nullable Drawable drawable) {
  if (drawable != null && backgroundTint != null) {
    drawable = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTintList(drawable, backgroundTint);
    DrawableCompat.setTintMode(drawable, backgroundTintMode);
  }
  super.setBackgroundDrawable(drawable);
}
 
Example 15
Source File: Preference.java    From AndroidMaterialPreferences with Apache License 2.0 5 votes vote down vote up
/**
 * Adapts the tint of the preference's icon.
 */
private void adaptIconTint() {
    Drawable icon = getIcon();

    if (icon != null) {
        DrawableCompat.setTintList(icon, tintList);
        DrawableCompat.setTintMode(icon, tintMode);
    }
}
 
Example 16
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setBackgroundTintList(@Nullable ColorStateList backgroundTint) {
  this.backgroundTint = backgroundTint;
  if (getBackground() != null) {
    Drawable wrappedBackground = DrawableCompat.wrap(getBackground().mutate());
    DrawableCompat.setTintList(wrappedBackground, backgroundTint);
    DrawableCompat.setTintMode(wrappedBackground, backgroundTintMode);
    if (wrappedBackground != getBackground()) {
      super.setBackgroundDrawable(wrappedBackground);
    }
  }
}
 
Example 17
Source File: ChipDrawable.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the checked icon's color tint using the specified {@link
 * android.content.res.ColorStateList}.
 *
 * @param checkedIconTint ColorStateList to tint the checked icon.
 * @attr ref com.google.android.material.R.styleable#Chip_checkedIconTint
 */
public void setCheckedIconTint(@Nullable ColorStateList checkedIconTint) {
  if (this.checkedIconTint != checkedIconTint) {
    this.checkedIconTint = checkedIconTint;

    if (canShowCheckedIcon()) {
      DrawableCompat.setTintList(checkedIcon, checkedIconTint);
    }

    onStateChange(getState());
  }
}
 
Example 18
Source File: FloatingActionButtonImpl.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
void setRippleColor(@Nullable ColorStateList rippleColor) {
  if (rippleDrawable != null) {
    DrawableCompat.setTintList(
        rippleDrawable, RippleUtils.sanitizeRippleDrawableColor(rippleColor));
  }
}
 
Example 19
Source File: BottomAppBar.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public BottomAppBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
  // Ensure we are using the correctly themed context rather than the context that was passed in.
  context = getContext();

  TypedArray a =
      ThemeEnforcement.obtainStyledAttributes(
          context, attrs, R.styleable.BottomAppBar, defStyleAttr, DEF_STYLE_RES);

  ColorStateList backgroundTint =
      MaterialResources.getColorStateList(context, a, R.styleable.BottomAppBar_backgroundTint);

  int elevation = a.getDimensionPixelSize(R.styleable.BottomAppBar_elevation, 0);
  float fabCradleMargin = a.getDimensionPixelOffset(R.styleable.BottomAppBar_fabCradleMargin, 0);
  float fabCornerRadius =
      a.getDimensionPixelOffset(R.styleable.BottomAppBar_fabCradleRoundedCornerRadius, 0);
  float fabVerticalOffset =
      a.getDimensionPixelOffset(R.styleable.BottomAppBar_fabCradleVerticalOffset, 0);
  fabAlignmentMode =
      a.getInt(R.styleable.BottomAppBar_fabAlignmentMode, FAB_ALIGNMENT_MODE_CENTER);
  fabAnimationMode =
      a.getInt(R.styleable.BottomAppBar_fabAnimationMode, FAB_ANIMATION_MODE_SCALE);
  hideOnScroll = a.getBoolean(R.styleable.BottomAppBar_hideOnScroll, false);
  // Reading out if we are handling bottom padding, so we can apply it to the FAB.
  paddingBottomSystemWindowInsets =
      a.getBoolean(R.styleable.BottomAppBar_paddingBottomSystemWindowInsets, false);
  paddingLeftSystemWindowInsets =
      a.getBoolean(R.styleable.BottomAppBar_paddingLeftSystemWindowInsets, false);
  paddingRightSystemWindowInsets =
      a.getBoolean(R.styleable.BottomAppBar_paddingRightSystemWindowInsets, false);

  a.recycle();

  fabOffsetEndMode =
      getResources().getDimensionPixelOffset(R.dimen.mtrl_bottomappbar_fabOffsetEndMode);

  EdgeTreatment topEdgeTreatment =
      new BottomAppBarTopEdgeTreatment(fabCradleMargin, fabCornerRadius, fabVerticalOffset);
  ShapeAppearanceModel shapeAppearanceModel =
      ShapeAppearanceModel.builder().setTopEdge(topEdgeTreatment).build();
  materialShapeDrawable.setShapeAppearanceModel(shapeAppearanceModel);
  materialShapeDrawable.setShadowCompatibilityMode(SHADOW_COMPAT_MODE_ALWAYS);
  materialShapeDrawable.setPaintStyle(Style.FILL);
  materialShapeDrawable.initializeElevationOverlay(context);
  setElevation(elevation);
  DrawableCompat.setTintList(materialShapeDrawable, backgroundTint);
  ViewCompat.setBackground(this, materialShapeDrawable);

  ViewUtils.doOnApplyWindowInsets(
      this,
      attrs,
      defStyleAttr,
      DEF_STYLE_RES,
      new ViewUtils.OnApplyWindowInsetsListener() {
        @NonNull
        @Override
        public WindowInsetsCompat onApplyWindowInsets(
            View view,
            @NonNull WindowInsetsCompat insets,
            @NonNull RelativePadding initialPadding) {
          // Just read the insets here. doOnApplyWindowInsets will apply the padding under the
          // hood.
          boolean leftInsetsChanged = false;
          boolean rightInsetsChanged = false;
          if (paddingBottomSystemWindowInsets) {
            bottomInset = insets.getSystemWindowInsetBottom();
          }
          if (paddingLeftSystemWindowInsets) {
            leftInsetsChanged = leftInset != insets.getSystemWindowInsetLeft();
            leftInset = insets.getSystemWindowInsetLeft();
          }
          if (paddingRightSystemWindowInsets) {
            rightInsetsChanged = rightInset != insets.getSystemWindowInsetRight();
            rightInset = insets.getSystemWindowInsetRight();
          }

          // We may need to change the position of the cutout or the action menu if the side
          // insets have changed.
          if (leftInsetsChanged || rightInsetsChanged) {
            cancelAnimations();

            setCutoutState();
            setActionMenuViewPosition();
          }

          return insets;
        }
      });
}
 
Example 20
Source File: DrawableHelper.java    From monero-wallet-android-app with MIT License 3 votes vote down vote up
/**
 * 对目标Drawable 进行着色
 *
 * @param drawable 目标Drawable
 * @param colors   着色值
 * @return 着色处理后的Drawable
 */
public static Drawable tintListDrawable(@NonNull Drawable drawable, ColorStateList colors) {
    Drawable wrappedDrawable = getCanTintDrawable(drawable);
    // 对 drawable 进行着色
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}