Java Code Examples for androidx.core.content.ContextCompat#getColor()

The following examples show how to use androidx.core.content.ContextCompat#getColor() . 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: AirQuality.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
@ColorInt
public int getPm25Color(Context context) {
    if (pm25 == null) {
        return Color.TRANSPARENT;
    } else if (pm25 <= 35) {
        return ContextCompat.getColor(context, R.color.colorLevel_1);
    } else if (pm25 <= 75) {
        return ContextCompat.getColor(context, R.color.colorLevel_2);
    } else if (pm25 <= 115) {
        return ContextCompat.getColor(context, R.color.colorLevel_3);
    } else if (pm25 <= 150) {
        return ContextCompat.getColor(context, R.color.colorLevel_4);
    } else if (pm25 <= 250) {
        return ContextCompat.getColor(context, R.color.colorLevel_5);
    } else {
        return ContextCompat.getColor(context, R.color.colorLevel_6);
    }
}
 
Example 2
Source File: CardPresenter.java    From androidtv-Leanback with Apache License 2.0 6 votes vote down vote up
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    mDefaultBackgroundColor =
        ContextCompat.getColor(parent.getContext(), R.color.default_background);
    mSelectedBackgroundColor =
            ContextCompat.getColor(parent.getContext(), R.color.selected_background);
    mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie, null);

    ImageCardView cardView = new ImageCardView(parent.getContext()) {
        @Override
        public void setSelected(boolean selected) {
            updateCardBackgroundColor(this, selected);
            super.setSelected(selected);
        }
    };

    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    updateCardBackgroundColor(cardView, false);
    return new ViewHolder(cardView);
}
 
Example 3
Source File: Pollen.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
@ColorInt
public static int getPollenColor(Context context, Integer level) {
    if (level == null) {
        return ContextCompat.getColor(context, R.color.colorLevel_1);
    } else if (level <= 1) {
        return ContextCompat.getColor(context, R.color.colorLevel_1);
    } else if (level <= 2) {
        return ContextCompat.getColor(context, R.color.colorLevel_2);
    } else if (level <= 3) {
        return ContextCompat.getColor(context, R.color.colorLevel_3);
    } else if (level <= 4) {
        return ContextCompat.getColor(context, R.color.colorLevel_4);
    } else if (level <= 5) {
        return ContextCompat.getColor(context, R.color.colorLevel_5);
    } else {
        return ContextCompat.getColor(context, R.color.colorLevel_6);
    }
}
 
Example 4
Source File: PictureMultiCuttingActivity.java    From Matisse-Kotlin with Apache License 2.0 6 votes vote down vote up
private void setupViews(@NonNull Intent intent) {
    scaleEnabled = intent.getBooleanExtra(UCropMulti.Options.EXTRA_SCALE, false);
    rotateEnabled = intent.getBooleanExtra(UCropMulti.Options.EXTRA_ROTATE, false);
    // 是否可拖动裁剪框
    isDragFrame = intent.getBooleanExtra(UCrop.Options.EXTRA_DRAG_CROP_FRAME, true);
    mActiveWidgetColor = intent.getIntExtra(UCropMulti.Options.EXTRA_UCROP_COLOR_WIDGET_ACTIVE, ContextCompat.getColor(this, R.color.ucrop_color_widget_active));
    mToolbarWidgetColor = intent.getIntExtra(UCropMulti.Options.EXTRA_UCROP_WIDGET_COLOR_TOOLBAR, ContextCompat.getColor(this, R.color.ucrop_color_toolbar_widget));
    if (mToolbarWidgetColor == 0) {
        mToolbarWidgetColor = ContextCompat.getColor(this, R.color.ucrop_color_toolbar_widget);
    }
    mToolbarCancelDrawable = intent.getIntExtra(UCropMulti.Options.EXTRA_UCROP_WIDGET_CANCEL_DRAWABLE, R.drawable.ucrop_ic_cross);
    mToolbarCropDrawable = intent.getIntExtra(UCropMulti.Options.EXTRA_UCROP_WIDGET_CROP_DRAWABLE, R.drawable.ucrop_ic_done);
    mToolbarTitle = intent.getStringExtra(UCropMulti.Options.EXTRA_UCROP_TITLE_TEXT_TOOLBAR);
    mToolbarTitle = mToolbarTitle != null ? mToolbarTitle : getResources().getString(R.string.ucrop_label_edit_photo);
    mLogoColor = intent.getIntExtra(UCropMulti.Options.EXTRA_UCROP_LOGO_COLOR, ContextCompat.getColor(this, R.color.ucrop_color_default_logo));
    mRootViewBackgroundColor = intent.getIntExtra(UCropMulti.Options.EXTRA_UCROP_ROOT_VIEW_BACKGROUND_COLOR, ContextCompat.getColor(this, R.color.ucrop_color_crop_background));
    setNavBarColor();
    setupAppBar();
    initiateRootViews();

    changeLayoutParams();
}
 
Example 5
Source File: NotiHelper.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
public static void initNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Context context = HiApplication.getAppContext();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
            int color = ContextCompat.getColor(context, R.color.icon_blue);
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(color);
            notificationChannel.enableVibration(false);
            notificationChannel.setBypassDnd(false);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
}
 
Example 6
Source File: CompactWidget.java    From Jockey with Apache License 2.0 6 votes vote down vote up
private RemoteViews createBaseView(Context context) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_compact);

    Intent launcherIntent = LibraryActivity.newNowPlayingIntent(context);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launcherIntent, 0);
    views.setOnClickPendingIntent(R.id.widget_compact_container, pendingIntent);

    views.setOnClickPendingIntent(R.id.widget_next, getSkipNextIntent(context));
    views.setOnClickPendingIntent(R.id.widget_play_pause, getPlayPauseIntent(context));
    views.setOnClickPendingIntent(R.id.widget_previous, getSkipPreviousIntent(context));

    @ColorInt int buttonColor = ContextCompat.getColor(context, R.color.widget_button);

    views.setInt(R.id.widget_next, "setColorFilter", buttonColor);
    views.setInt(R.id.widget_play_pause, "setColorFilter", buttonColor);
    views.setInt(R.id.widget_previous, "setColorFilter", buttonColor);

    return views;
}
 
Example 7
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 8
Source File: FiveDayChart.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
public FiveDayChart(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    LayoutInflater.from(context).inflate(R.layout.view_time, this);
    lineChart = (TimeLineChart) findViewById(R.id.line_chart);
    barChart = (TimeBarChart) findViewById(R.id.bar_chart);
    cirCleView = (FrameLayout) findViewById(R.id.circle_frame_time);

    EventBus.getDefault().register(this);

    colorArray = new int[]{ContextCompat.getColor(mContext, R.color.up_color), ContextCompat.getColor(mContext, R.color.equal_color), ContextCompat.getColor(mContext, R.color.down_color)};

    playHeartbeatAnimation(cirCleView.findViewById(R.id.anim_view));

}
 
Example 9
Source File: TapTarget.java    From TapTargetView with Apache License 2.0 5 votes vote down vote up
@Nullable
private Integer colorResOrInt(Context context, @Nullable Integer value, @ColorRes int resource) {
  if (resource != -1) {
    return ContextCompat.getColor(context, resource);
  }

  return value;
}
 
Example 10
Source File: MainActivity.java    From android-speech with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Speech.init(this, getPackageName());

    linearLayout = findViewById(R.id.linearLayout);

    button = findViewById(R.id.button);
    button.setOnClickListener(view -> onButtonClick());

    speak = findViewById(R.id.speak);
    speak.setOnClickListener(view -> onSpeakClick());

    text = findViewById(R.id.text);
    textToSpeech = findViewById(R.id.textToSpeech) ;
    progress = findViewById(R.id.progress);

    int[] colors = {
            ContextCompat.getColor(this, android.R.color.black),
            ContextCompat.getColor(this, android.R.color.darker_gray),
            ContextCompat.getColor(this, android.R.color.black),
            ContextCompat.getColor(this, android.R.color.holo_orange_dark),
            ContextCompat.getColor(this, android.R.color.holo_red_dark)
    };
    progress.setColors(colors);
}
 
Example 11
Source File: TrendLinearLayout.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void setColor(boolean lightTheme) {
    if (lightTheme) {
        lineColor = ColorUtils.setAlphaComponent(Color.BLACK, (int) (255 * 0.05));
        textColor = ContextCompat.getColor(getContext(), R.color.colorTextSubtitle_light);
    } else {
        lineColor = ColorUtils.setAlphaComponent(Color.WHITE, (int) (255 * 0.1));
        textColor = ContextCompat.getColor(getContext(), R.color.colorTextSubtitle_dark);
    }
}
 
Example 12
Source File: TagColorUtil.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
public static int getTextColor(Context context, int level, boolean expand) {
    SparseIntArray map = expand ? TEXT_COLOR_EXPAND : TEXT_COLOR;
    Integer result = map.get(level);
    if (result == null) {
        result = map.get(Log.VERBOSE);
    }
    return ContextCompat.getColor(context, result);
}
 
Example 13
Source File: FragmentCharacteristicDetail.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO inflate appropriate layout file
    viewBackgroundColor = ContextCompat.getColor(getActivity(), R.color.silabs_white);

    handler = new Handler();

    View view = inflater.inflate(R.layout.fragment_characteristic_details, container, false);
    fragmentRootView = view;
    viewGroup = container;
    context = getActivity();
    defaultMargin = getResources().getDimensionPixelSize(R.dimen.characteristic_text_left_margin);

    valuesLayout = view.findViewById(R.id.values_layout);

    mDevice = ((DeviceServicesActivity) getActivity()).getBluetoothGatt();
    mCharact = Engine.getInstance().getCharacteristic(mBluetoothCharact.getUuid());
    mService = Engine.getInstance().getService(mBluetoothCharact.getService().getUuid());
    mDescriptors = new ArrayList<>();
    setProperties();

    Log.d("Charac", mBluetoothCharact.getUuid().toString() + " " + mBluetoothCharact.getInstanceId());

    mBluetoothCharact.getProperties();
    configureWriteable();

    updateBall();

    if (!isRawValue) {
        prepareValueData();
    }
    loadValueViews();

    if (displayWriteDialog) {
        showCharacteristicWriteDialog();
    }

    return view;
}
 
Example 14
Source File: TintHelper.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("PrivateResource")
@ColorInt
private static int getDefaultRippleColor(@NonNull Context context, boolean useDarkRipple) {
    // Light ripple is actually translucent black, and vice versa
    return ContextCompat.getColor(context, useDarkRipple ?
            R.color.ripple_material_light : R.color.ripple_material_dark);
}
 
Example 15
Source File: SnowImplementor.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
@ColorInt
public static int getThemeColor(Context context, @TypeRule int type) {
    switch (type) {
        case TYPE_SNOW_DAY:
            return Color.rgb(104, 186, 255);

        case TYPE_SNOW_NIGHT:
            return Color.rgb(26, 91, 146);
    }
    return ContextCompat.getColor(context, R.color.colorPrimary);
}
 
Example 16
Source File: MainActivity.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
private void getWhiteStyle() {
        // 相册主题
        mPictureParameterStyle = new PictureParameterStyle();
        // 是否改变状态栏字体颜色(黑白切换)
        mPictureParameterStyle.isChangeStatusBarFontColor = true;
        // 是否开启右下角已完成(0/9)风格
        mPictureParameterStyle.isOpenCompletedNumStyle = false;
        // 是否开启类似QQ相册带数字选择风格
        mPictureParameterStyle.isOpenCheckNumStyle = false;
        // 相册状态栏背景色
        mPictureParameterStyle.pictureStatusBarColor = Color.parseColor("#FFFFFF");
        // 相册列表标题栏背景色
        mPictureParameterStyle.pictureTitleBarBackgroundColor = Color.parseColor("#FFFFFF");
        // 相册列表标题栏右侧上拉箭头
        mPictureParameterStyle.pictureTitleUpResId = R.drawable.ic_orange_arrow_up;
        // 相册列表标题栏右侧下拉箭头
        mPictureParameterStyle.pictureTitleDownResId = R.drawable.ic_orange_arrow_down;
        // 相册文件夹列表选中圆点
        mPictureParameterStyle.pictureFolderCheckedDotStyle = R.drawable.picture_orange_oval;
        // 相册返回箭头
        mPictureParameterStyle.pictureLeftBackIcon = R.drawable.ic_back_arrow;
        // 标题栏字体颜色
        mPictureParameterStyle.pictureTitleTextColor = ContextCompat.getColor(getContext(), R.color.app_color_black);
        // 相册右侧取消按钮字体颜色  废弃 改用.pictureRightDefaultTextColor和.pictureRightDefaultTextColor
        mPictureParameterStyle.pictureCancelTextColor = ContextCompat.getColor(getContext(), R.color.app_color_black);
        // 选择相册目录背景样式
        mPictureParameterStyle.pictureAlbumStyle = R.drawable.picture_new_item_select_bg;
        // 相册列表勾选图片样式
        mPictureParameterStyle.pictureCheckedStyle = R.drawable.picture_checkbox_selector;
        // 相册列表底部背景色
        mPictureParameterStyle.pictureBottomBgColor = ContextCompat.getColor(getContext(), R.color.picture_color_fa);
        // 已选数量圆点背景样式
        mPictureParameterStyle.pictureCheckNumBgStyle = R.drawable.picture_num_oval;
        // 相册列表底下预览文字色值(预览按钮可点击时的色值)
        mPictureParameterStyle.picturePreviewTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_fa632d);
        // 相册列表底下不可预览文字色值(预览按钮不可点击时的色值)
        mPictureParameterStyle.pictureUnPreviewTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_9b);
        // 相册列表已完成色值(已完成 可点击色值)
        mPictureParameterStyle.pictureCompleteTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_fa632d);
        // 相册列表未完成色值(请选择 不可点击色值)
        mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_9b);
        // 预览界面底部背景色
        mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(getContext(), R.color.picture_color_white);
        // 原图按钮勾选样式  需设置.isOriginalImageControl(true); 才有效
        mPictureParameterStyle.pictureOriginalControlStyle = R.drawable.picture_original_checkbox;
        // 原图文字颜色 需设置.isOriginalImageControl(true); 才有效
        mPictureParameterStyle.pictureOriginalFontColor = ContextCompat.getColor(getContext(), R.color.app_color_53575e);
        // 外部预览界面删除按钮样式
        mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_black_delete;
        // 外部预览界面是否显示删除按钮
        mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;
//        // 自定义相册右侧文本内容设置
//        mPictureParameterStyle.pictureRightDefaultText = "";
//        // 自定义相册未完成文本内容
//        mPictureParameterStyle.pictureUnCompleteText = "";
//        // 自定义相册完成文本内容
//        mPictureParameterStyle.pictureCompleteText = "";
//        // 自定义相册列表不可预览文字
//        mPictureParameterStyle.pictureUnPreviewText = "";
//        // 自定义相册列表预览文字
//        mPictureParameterStyle.picturePreviewText = "";

//        // 自定义相册标题字体大小
//        mPictureParameterStyle.pictureTitleTextSize = 18;
//        // 自定义相册右侧文字大小
//        mPictureParameterStyle.pictureRightTextSize = 14;
//        // 自定义相册预览文字大小
//        mPictureParameterStyle.picturePreviewTextSize = 14;
//        // 自定义相册完成文字大小
//        mPictureParameterStyle.pictureCompleteTextSize = 14;
//        // 自定义原图文字大小
//        mPictureParameterStyle.pictureOriginalTextSize = 14;

        // 裁剪主题
        mCropParameterStyle = new PictureCropParameterStyle(
                ContextCompat.getColor(getContext(), R.color.app_color_white),
                ContextCompat.getColor(getContext(), R.color.app_color_white),
                ContextCompat.getColor(getContext(), R.color.app_color_black),
                mPictureParameterStyle.isChangeStatusBarFontColor);
    }
 
Example 17
Source File: BaseViewModel.java    From Jockey with Apache License 2.0 4 votes vote down vote up
@ColorInt
protected int getColor(@ColorRes int colorRes) {
    return ContextCompat.getColor(mContext, colorRes);
}
 
Example 18
Source File: ApplicationContextHolder.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 4 votes vote down vote up
public static int getColor(@ColorRes int resId) {
    return ContextCompat.getColor(sContext, resId);
}
 
Example 19
Source File: StorageGraphView.java    From mollyim-android with GNU General Public License v3.0 3 votes vote down vote up
private void initialize() {
  setWillNotDraw(false);
  paint.setStyle(Paint.Style.FILL);

  Entry emptyEntry = new Entry(ContextCompat.getColor(getContext(), R.color.storage_color_empty), 1);

  emptyBreakdown = new StorageBreakdown(Collections.singletonList(emptyEntry));

  setStorageBreakdown(emptyBreakdown);
}
 
Example 20
Source File: HighlightGuideView.java    From QuickDevFramework with Apache License 2.0 2 votes vote down vote up
/**
 * 设置蒙版背景颜色
 *
 * @param resId 颜色资源ID
 * */
public HighlightGuideView setMaskBackgroundRes(@ColorRes int resId) {
    backgroundColor = ContextCompat.getColor(getContext(), resId);
    return this;
}