androidx.annotation.ColorRes Java Examples

The following examples show how to use androidx.annotation.ColorRes. 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: DrawableHelper.java    From GetApk with MIT License 6 votes vote down vote up
@Nullable
public static Drawable tintDrawable(@NonNull Context context, @DrawableRes int resId, @ColorRes int tint, PorterDuff.Mode tintMode) {
    ColorStateList tintList = ContextCompat.getColorStateList(context, tint);
    Drawable drawable = ContextCompat.getDrawable(context, resId);
    if (tintList != null && drawable != null) {
        // First mutate the Drawable, then wrap it and set the tint list

        if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
            drawable = drawable.mutate();
        }
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, tintList);

        if (tintMode != null) {
            DrawableCompat.setTintMode(drawable, tintMode);
        }
    }
    return drawable;
}
 
Example #2
Source File: ViewUtils.java    From animation-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Create a color change animation over a foreground property of a {@link FrameLayout}.
 *
 * @param target The target view.
 * @param startColorRes The color to start from.
 * @param targetColorRes The color this animation will end with.
 * @param interpolator The interpolator to use.
 * @return The color change animation.
 */
@NonNull
public static ObjectAnimator createColorChange(@NonNull FrameLayout target,
                                               @ColorRes int startColorRes,
                                               @ColorRes int targetColorRes,
                                               @NonNull Interpolator interpolator) {
    Context context = target.getContext();
    final int startColor = ContextCompat.getColor(context, startColorRes);
    final int targetColor = ContextCompat.getColor(context, targetColorRes);
    ObjectAnimator colorChange = ObjectAnimator.ofInt(target,
            ViewUtils.FOREGROUND_COLOR, startColor, targetColor);
    colorChange.setEvaluator(new ArgbEvaluator());
    colorChange.setInterpolator(interpolator);
    colorChange.setDuration(context.getResources()
            .getInteger(android.R.integer.config_longAnimTime));
    return colorChange;
}
 
Example #3
Source File: OvalGuideRenderer.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public OvalGuideRenderer(@ColorRes int color) {
  this.ovalGuideColor = color;

  this.paint = new Paint();
  this.paint.setStyle(Paint.Style.STROKE);
  this.paint.setAntiAlias(true);
}
 
Example #4
Source File: OngoingCallActivity.java    From call_manage with MIT License 5 votes vote down vote up
private void setData(long millisInFuture, boolean isRejecting) {
    mIsRejecting = isRejecting;
    @ColorRes int textColorRes;
    @StringRes int textIndicator;
    if (isRejecting) {
        textColorRes = R.color.red_phone;
        textIndicator = R.string.reject_timer_indicator;
    } else {
        textColorRes = R.color.green_phone;
        textIndicator = R.string.answer_timer_indicator;
    }

    @ColorInt int textColor = ContextCompat.getColor(OngoingCallActivity.this, textColorRes);
    mActionTimeLeftText.setTextColor(textColor);
    mTimerIndicatorText.setText(textIndicator);

    mTimer = new CountDownTimer(millisInFuture, REFRESH_RATE) {
        Locale mLocale = Locale.getDefault();

        @Override
        public void onTick(long millisUntilFinished) {
            int secondsUntilFinished = (int) (millisUntilFinished / 1000);
            String timer = String.format(mLocale, "00:%02d", secondsUntilFinished);
            mActionTimeLeftText.setText(timer);
        }

        @Override
        public void onFinish() {
            end();
        }
    };
}
 
Example #5
Source File: StateListUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 创建 ColorStateList
 * @param selected 选中状态
 * @param pressed  按下状态
 * @param normal   默认状态
 * @return {@link ColorStateList}
 */
public static ColorStateList createColorStateList(@ColorRes final int selected, @ColorRes final int pressed, @ColorRes final int normal) {
    // 颜色值
    int[] colors = new int[3];
    colors[0] = ResourceUtils.getColor(selected);
    colors[1] = ResourceUtils.getColor(pressed);
    colors[2] = ResourceUtils.getColor(normal);
    // 状态值
    int[][] states = new int[3][];
    states[0] = new int[]{android.R.attr.state_selected};
    states[1] = new int[]{android.R.attr.state_pressed};
    states[2] = new int[]{};
    // 生成 ColorStateList
    return new ColorStateList(states, colors);
}
 
Example #6
Source File: CatComboAdapter.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CatComboAdapter(@NonNull Context context, int resource, int textViewResourceId,
                       @NonNull List<CategoryOptionCombo> objects,
                       String categoryOptionName,
                       @ColorRes int textColor) {
    super(context, resource, textViewResourceId, objects);
    this.options = objects;
    this.catComboName = categoryOptionName;
    this.textColor = textColor;
}
 
Example #7
Source File: ViewUtils.java    From animation-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Set the status bar color of an activity to a specified value.
 *
 * @param activity The activity to set the colorResId for.
 * @param colorResId The value to use.
 */
public static void setStatusBarColor(@NonNull Activity activity, @ColorRes int colorResId) {
    //noinspection ConstantConditions
    if (activity == null) {
        return;
    }
    final int backgroundColor = ContextCompat.getColor(activity, colorResId);
    activity.getWindow().setStatusBarColor(backgroundColor);
}
 
Example #8
Source File: ObjectStyleUtils.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int getColorResource(Context context, String styleColor, @ColorRes int defaultColorResource) {
    if (styleColor == null) {
        return ContextCompat.getColor(context, defaultColorResource);
    } else {
        String color = styleColor.startsWith("#") ? styleColor : "#" + styleColor;
        int colorRes;
        if (color.length() == 4)
            return ContextCompat.getColor(context, defaultColorResource);
        else
            colorRes = Color.parseColor(color);

        return colorRes;
    }
}
 
Example #9
Source File: KToast.java    From KToast with Apache License 2.0 5 votes vote down vote up
/**
 * Custom toast background color.
 * @param activity
 * @param message
 * @param gravity
 * @param duration
 * @param toastColor
 */
public static void customColorToast(final Activity activity, String message, final int gravity, int duration, @ColorRes int toastColor){
    final View view = (activity.getLayoutInflater().inflate(R.layout.layout_custom_toast, null));

    ((TextView)view.findViewById(R.id.txtCustomToast)).setText(message);

    Drawable customBackground = view.getResources().getDrawable(R.drawable.background_custom_toast);
    customBackground.setColorFilter(ContextCompat.getColor(view.getContext(), toastColor), PorterDuff.Mode.ADD);

    view.findViewById(R.id.customToastLyt).setBackground(customBackground);

    if (duration == LENGTH_AUTO){
        duration = Util.toastTime(message);
    }

    new CountDownTimer(Math.max(duration+1000, 1000), 2000){
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast toast = new Toast(activity);
            toast.setGravity(gravity, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        }
    }.start();
}
 
Example #10
Source File: ShapeUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 设置边框颜色
 * @param width 描边的宽度
 * @param color 描边的颜色
 * @return {@link ShapeUtils.Builder}
 */
public Builder setStroke(final int width, @ColorRes final int color) {
    if (gradientDrawable != null) {
        try {
            gradientDrawable.setStroke(width, ContextCompat.getColor(DevUtils.getContext(), color));
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "setStroke");
        }
    }
    return this;
}
 
Example #11
Source File: ImmersionBar.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 解决布局与状态栏重叠问题,支持侧滑返回
 * Fits system windows immersion bar.
 *
 * @param fits                               the fits
 * @param statusBarColorContentView          the status bar color content view 状态栏颜色
 * @param statusBarColorContentViewTransform the status bar color content view transform  状态栏变色后的颜色
 * @param statusBarContentViewAlpha          the status bar content view alpha  透明度
 * @return the immersion bar
 */
public ImmersionBar fitsSystemWindows(boolean fits, @ColorRes int statusBarColorContentView
        , @ColorRes int statusBarColorContentViewTransform, @FloatRange(from = 0f, to = 1f) float statusBarContentViewAlpha) {
    mBarParams.fits = fits;
    mBarParams.statusBarColorContentView = ContextCompat.getColor(mActivity, statusBarColorContentView);
    mBarParams.statusBarColorContentViewTransform = ContextCompat.getColor(mActivity, statusBarColorContentViewTransform);
    mBarParams.statusBarContentViewAlpha = statusBarContentViewAlpha;
    mBarParams.statusBarColorContentView = ContextCompat.getColor(mActivity, statusBarColorContentView);
    mContentView.setBackgroundColor(ColorUtils.blendARGB(mBarParams.statusBarColorContentView,
            mBarParams.statusBarColorContentViewTransform, mBarParams.statusBarContentViewAlpha));
    return this;
}
 
Example #12
Source File: RecyclerMessageView.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
public void enableSwipeRefresh(@NonNull SwipeRefreshLayout.OnRefreshListener listener, @ColorRes int... colors) {
    if (colors.length <= 0) throw new IllegalArgumentException("Provide at least one color!");

    swipeRefresh.setEnabled(true);
    swipeRefresh.setColorSchemeResources(colors);
    swipeRefresh.setOnRefreshListener(listener);
    list.setVisibility(VISIBLE);
}
 
Example #13
Source File: Service.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Finds and returns an attribute color. If it was not found the method returns the default color
 */
public static int getColorAttribute(@AttrRes int attribute, @ColorRes int defaultColor, Context context) {
    TypedValue a = new TypedValue();
    context.getTheme().resolveAttribute(attribute, a, true);
    if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        return a.data;
    } else {
        return ContextCompat.getColor(context, defaultColor);
    }
}
 
Example #14
Source File: BackgroundHelper.java    From monero-wallet-android-app with MIT License 5 votes vote down vote up
public static Drawable getButtonBackground(Context context, @ColorRes int colorRes, @ColorRes int enabledColorRes) {
    Drawable drawable = DrawableHelper.getSolidShapeDrawable(ContextCompat.getColor(context, colorRes), DisplayHelper.dpToPx(25));
    Drawable drawableEnabled = DrawableHelper.getSolidShapeDrawable(ContextCompat.getColor(context, enabledColorRes), DisplayHelper.dpToPx(25));
    return SelectorFactory.newGeneralSelector()
            .setDefaultDrawable(drawableEnabled)
            .setDisabledDrawable(drawable)
            .create();
}
 
Example #15
Source File: BackgroundHelper.java    From monero-wallet-android-app with MIT License 5 votes vote down vote up
public static Drawable getDotDrawable(Context context, @ColorRes int colorRes, int diameter) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.OVAL);
    gradientDrawable.setColor(ContextCompat.getColor(context, colorRes));
    gradientDrawable.setSize(diameter, diameter);
    return gradientDrawable;
}
 
Example #16
Source File: DisplayLeakAdapter.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
private static String hexStringColor(Resources resources, @ColorRes int colorResId) {
    return String.format("#%06X", (0xFFFFFF & resources.getColor(colorResId)));
}
 
Example #17
Source File: DonutProgress.java    From Aria2App with GNU General Public License v3.0 4 votes vote down vote up
public void setFinishedStrokeColorRes(@ColorRes int color) {
    setFinishedStrokeColor(ContextCompat.getColor(getContext(), color));
}
 
Example #18
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
public T colorResId(@ColorRes int colorId) {
    return color(ContextCompat.getColor(mContext, colorId));
}
 
Example #19
Source File: ThemeStore.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ThemeStore navigationBarColorRes(@ColorRes int colorRes) {
    return navigationBarColor(ContextCompat.getColor(mContext, colorRes));
}
 
Example #20
Source File: ModalBottomSheetHeaderView.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public void setBackgroundColorRes(@ColorRes int color) {
    setBackgroundColor(ContextCompat.getColor(getContext(), color));
}
 
Example #21
Source File: CircularProgressView.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
/**
 * You can simulate the use of this method with by calling {@link #setColor(int)} with ContextCompat:
 * setBackgroundColor(ContextCompat.getColor(resId));
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public void setColorResource(@ColorRes int resId) {
    setColor(getContext().getColor(resId));
}
 
Example #22
Source File: CircleImageView.java    From Shipr-Community-Android with GNU General Public License v3.0 4 votes vote down vote up
public void setCircleBackgroundColorResource(@ColorRes int circleBackgroundRes) {
    setCircleBackgroundColor(getContext().getResources().getColor(circleBackgroundRes));
}
 
Example #23
Source File: CircleImageView.java    From Shipr-Community-Android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @deprecated Use {@link #setBorderColor(int)} instead
 */
@Deprecated
public void setBorderColorResource(@ColorRes int borderColorRes) {
    setBorderColor(getContext().getResources().getColor(borderColorRes));
}
 
Example #24
Source File: MainActivity.java    From Hify with MIT License 4 votes vote down vote up
@ColorInt
private int color(@ColorRes int res) {
    return ContextCompat.getColor(this, res);
}
 
Example #25
Source File: FakeCallsRecyclerViewAdapter.java    From BaldPhone with Apache License 2.0 4 votes vote down vote up
public void setType(int type) {
    @ColorRes final int colorRes;
    @DrawableRes final int drawableRes;
    @StringRes final int stringRes;
    switch (type) {
        case INCOMING_TYPE:
        case ANSWERED_EXTERNALLY_TYPE:
            drawableRes = R.drawable.call_received_on_button;
            stringRes = R.string.received;
            colorRes = R.color.received;
            break;
        case MISSED_TYPE:
        case REJECTED_TYPE:
            drawableRes = R.drawable.call_missed_on_button;
            stringRes = R.string.missed;
            colorRes = R.color.missed;
            break;
        case OUTGOING_TYPE:
            drawableRes = R.drawable.call_made_on_button;
            stringRes = R.string.outgoing;
            colorRes = R.color.outgoing;
            break;
        case VOICEMAIL_TYPE:
            drawableRes = R.drawable.voicemail_on_button;
            stringRes = R.string.voice_mail;
            colorRes = R.color.other;
            break;
        case BLOCKED_TYPE:
            drawableRes = R.drawable.blocked_on_button;
            stringRes = R.string.blocked;
            colorRes = R.color.other;
            break;
        default:
            drawableRes = R.drawable.error_on_background;
            stringRes = R.string.empty;
            colorRes = R.color.other;
    }
    tv_type.setText(stringRes);
    iv_type.setImageResource(drawableRes);
    this.fl_contact_only.setBackgroundResource(colorRes);

}
 
Example #26
Source File: ThemeItem.java    From SmsCode with GNU General Public License v3.0 4 votes vote down vote up
public ThemeItem(@StringRes int colorNameRes, @ColorRes int colorValueRes, @StyleRes int themeRes) {
    this.colorNameRes = colorNameRes;
    this.colorValueRes = colorValueRes;
    this.themeRes = themeRes;
}
 
Example #27
Source File: Utils.java    From Aria2App with GNU General Public License v3.0 4 votes vote down vote up
public static void setupChart(@NonNull LineChart chart, boolean small, @ColorRes @Nullable Integer fgColorRes) {
    chart.clear();

    int fgColor;
    Context context = chart.getContext();
    if (fgColorRes == null)
        fgColor = CommonUtils.resolveAttrAsColor(context, R.attr.colorOnSurface);
    else
        fgColor = ContextCompat.getColor(context, fgColorRes);

    chart.setDescription(null);
    chart.setDrawGridBackground(false);
    chart.setBackgroundColor(Color.alpha(0));
    chart.setTouchEnabled(false);

    Legend legend = chart.getLegend();
    legend.setTextColor(fgColor);
    legend.setEnabled(true);

    LineData data = new LineData();
    data.setValueTextColor(fgColor);
    chart.setData(data);

    YAxis ya = chart.getAxisLeft();
    ya.setAxisLineColor(fgColor);
    ya.setTextColor(fgColor);
    ya.setTextSize(small ? 8 : 9);
    ya.setAxisMinimum(0);
    ya.setDrawAxisLine(false);
    ya.setLabelCount(small ? 4 : 8, true);
    ya.setEnabled(true);
    ya.setDrawGridLines(true);
    ya.setGridColor(fgColor);
    ya.setValueFormatter(new CustomYAxisValueFormatter());

    chart.getAxisRight().setEnabled(false);
    chart.getXAxis().setEnabled(false);

    data.addDataSet(initUploadSet(context));
    data.addDataSet(initDownloadSet(context));

    chart.invalidate();
}
 
Example #28
Source File: ThemeStore.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ThemeStore statusBarColorRes(@ColorRes int colorRes) {
    return statusBarColor(ContextCompat.getColor(mContext, colorRes));
}
 
Example #29
Source File: ThemeStore.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ThemeStore primaryColorRes(@ColorRes int colorRes) {
    return primaryColor(ContextCompat.getColor(mContext, colorRes));
}
 
Example #30
Source File: CommonUtils.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public static void setImageTintColor(@NonNull ImageView view, @ColorRes int res) {
    view.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(view.getContext(), res)));
}