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

The following examples show how to use androidx.core.content.ContextCompat#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: ThemeUtil.java    From msdkui-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a color value specified by the given theme attribute.
 *
 * @param context
 *         the required context.
 * @param colorAttribute
 *         a theme attribute such as <code>R.attr.colorBackground</code>.
 *
 * @return a color. In case of errors <code>Color.CYAN</code> is returned.
 */
@ColorInt
public static int getColor(Context context, int colorAttribute) {
    final TypedValue outValue = new TypedValue();
    context.getTheme().resolveAttribute(colorAttribute, outValue, true);
    if (isColor(outValue)) {
        return outValue.data;
    } else if (outValue.type == TypedValue.TYPE_REFERENCE || outValue.type == TypedValue.TYPE_STRING) {
        try {
            final ColorStateList colors = ContextCompat.getColorStateList(context, outValue.resourceId);
            return colors.getDefaultColor();
        } catch (Resources.NotFoundException ex) {
            Log.d(ThemeUtil.class.getName(), ex.getMessage());
        }
    } else if (outValue.type == TypedValue.TYPE_NULL) {
        return getColorFromStyleable(context, styleableForAttr(colorAttribute));
    }
    // default to cyan in case of errors
    return Color.CYAN;
}
 
Example 2
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 3
Source File: DrawableHelper.java    From GetApk with MIT License 6 votes vote down vote up
public static Drawable tintDrawable(@NonNull Context context, @NonNull Drawable drawable, @ColorRes int tint, PorterDuff.Mode tintMode) {
    ColorStateList tintList = ContextCompat.getColorStateList(context, tint);
    if (tintList != 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 4
Source File: SkinManager.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
public ColorStateList getColorStateList(int resId) {
        if (isLocal) {
            CommonLogger.e("放回本地");
            return ContextCompat.getColorStateList(context, resId);
        }
        int id = resources.getIdentifier(context.getResources().getResourceEntryName(resId), "color", packageName);
        if (id == 0) {
//            如果找不到资源,返回本地资源
            return ContextCompat.getColorStateList(context, resId);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return resources.getColorStateList(id, null);
            } else {
                return resources.getColorStateList(id);
            }
        }
    }
 
Example 5
Source File: FloatingActionButtonTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
@SmallTest
public void testSetStatefulTintAcrossStateChanges() {
  final Activity activity = activityTestRule.getActivity();

  final ColorStateList tint = ContextCompat.getColorStateList(activity, R.color.fab_tint);
  final int normal = ContextCompat.getColor(activity, R.color.sand_default);
  final int notSelected = ContextCompat.getColor(activity, R.color.sand_disabled);

  // First set the background tint list to the ColorStateList
  onView(withId(R.id.fab_standard)).perform(setBackgroundTintList(tint));

  // Assert that the background is tinted correctly across state changes
  onView(withId(R.id.fab_standard))
      .perform(setSelected(true))
      .check(matches(withFabBackgroundFill(normal)))
      .perform(setSelected(false))
      .check(matches(withFabBackgroundFill(notSelected)))
      .perform(setSelected(true))
      .check(matches(withFabBackgroundFill(normal)));
}
 
Example 6
Source File: NumberPicker.java    From PhoneProfilesPlus with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates a NumberPicker object
 *
 * @param context the Context required for creation
 * @param attrs   additional attributes that define custom colors, selectors, and backgrounds.
 */
public NumberPicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    //LayoutInflater layoutInflater =
    //        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LayoutInflater.from(context).inflate(getLayoutId(), this);

    // Init defaults
    mTextColor = ContextCompat.getColorStateList(context, R.color.dialog_text_color_holo_dark);
            //getResources().getColorStateList(R.color.dialog_text_color_holo_dark);
    mKeyBackgroundResId = R.drawable.key_background_dark;
    mButtonBackgroundResId = R.drawable.button_background_dark;
    mBackspaceDrawableSrcResId = R.drawable.ic_backspace_dark_bp;
    mClearDrawableSrcResId = R.drawable.ic_clear_dark_bp;
    mDividerColor = ContextCompat.getColor(context, R.color.default_divider_color_dark);
            //getResources().getColor(R.color.default_divider_color_dark);
}
 
Example 7
Source File: ResourceUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 ColorStateList
 * @param id resource identifier of a {@link ColorStateList}
 * @return {@link ColorStateList}
 */
public static ColorStateList getColorStateList(@ColorRes final int id) {
    try {
        return ContextCompat.getColorStateList(DevUtils.getContext(), id);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getColorStateList");
    }
    return null;
}
 
Example 8
Source File: SkinCompatResources.java    From Android-skin-support with MIT License 5 votes vote down vote up
public ColorStateList getColorStateList(int resId) {
    ColorStateList colorStateList = ContextCompat.getColorStateList(mAppContext, resId);
    if (isDefaultSkin) {
        return colorStateList;
    }

    int targetResId = getTargetResId(resId);
    return targetResId == 0 ? colorStateList : mResources.getColorStateList(targetResId);
}
 
Example 9
Source File: MarkerButton.java    From ToggleButtonGroup with Apache License 2.0 5 votes vote down vote up
public MarkerButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.view_marker_button, this, true);
    mIvBg = (ImageView) findViewById(R.id.iv_bg);
    mTvText = (TextView) findViewById(R.id.tv_text);

    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs, R.styleable.MarkerButton, 0, 0);
    try {
        CharSequence text = a.getText(R.styleable.MarkerButton_android_text);
        mTvText.setText(text);

        ColorStateList colors = a.getColorStateList(R.styleable.MarkerButton_android_textColor);
        if (colors == null) {
            colors = ContextCompat.getColorStateList(context, R.color.selector_marker_text);
        }
        mTvText.setTextColor(colors);

        float textSize = a.getDimension(R.styleable.MarkerButton_android_textSize, dpToPx(DEFAULT_TEXT_SIZE_SP));
        mTvText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        mMarkerColor = a.getColor(R.styleable.MarkerButton_tbgMarkerColor, ContextCompat.getColor(getContext(), R.color.tbg_color_default_marker));

        mRadioStyle = a.getBoolean(R.styleable.MarkerButton_tbgRadioStyle, false);
    } finally {
        a.recycle();
    }
}
 
Example 10
Source File: ActionPreference.java    From AndroidMaterialPreferences with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains the text color of the preference's title from a specific typed array.
 *
 * @param typedArray
 *         The typed array, the color should be obtained from, as an instance of the class
 *         {@link TypedArray}. The typed array may not be null
 */
private void obtainTextColor(@NonNull final TypedArray typedArray) {
    ColorStateList colorStateList =
            typedArray.getColorStateList(R.styleable.ActionPreference_android_textColor);

    if (colorStateList == null) {
        colorStateList = ContextCompat
                .getColorStateList(getContext(), R.color.action_preference_text_color);
    }

    setTextColor(colorStateList);
}
 
Example 11
Source File: AudioPlayer.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private boolean init(ViewHolder viewHolder, Message message) {
    messageAdapter.getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
    if (viewHolder.darkBackground) {
        viewHolder.runtime.setTextAppearance(this.messageAdapter.getContext(), R.style.TextAppearance_Conversations_Caption_OnDark);
    } else {
        viewHolder.runtime.setTextAppearance(this.messageAdapter.getContext(), R.style.TextAppearance_Conversations_Caption);
    }
    viewHolder.progress.setOnSeekBarChangeListener(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ColorStateList color = viewHolder.darkBackground ? ContextCompat.getColorStateList(messageAdapter.getContext(), R.color.white70) : viewHolder.isOrange ? ContextCompat.getColorStateList(messageAdapter.getContext(), R.color.darkorange) : ContextCompat.getColorStateList(messageAdapter.getContext(), R.color.darkblue);
        viewHolder.progress.setThumbTintList(color);
        viewHolder.progress.setProgressTintList(color);
    }
    viewHolder.playPause.setAlpha(viewHolder.darkBackground ? 0.7f : 0.57f);
    viewHolder.playPause.setOnClickListener(this);
    if (message == currentlyPlayingMessage) {
        if (AudioPlayer.player != null && AudioPlayer.player.isPlaying()) {
            viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_pause_white_36dp : R.drawable.ic_pause_black_36dp);
            viewHolder.progress.setEnabled(true);
        } else {
            viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_play_arrow_white_36dp : R.drawable.ic_play_arrow_black_36dp);
            viewHolder.progress.setEnabled(false);
        }
        return true;
    } else {
        viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_play_arrow_white_36dp : R.drawable.ic_play_arrow_black_36dp);
        viewHolder.runtime.setText(formatTime(message.getFileParams().runtime));
        viewHolder.progress.setProgress(0);
        viewHolder.progress.setEnabled(false);
        return false;
    }
}
 
Example 12
Source File: NumberView.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate a NumberView
 *
 * @param context the Context in which to inflate the View
 * @param attrs attributes that define the title color
 */
public NumberView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mAndroidClockMonoThin =
            Typeface.createFromAsset(context.getAssets(), "fonts/AndroidClockMono-Thin.ttf");

    // Init defaults
    mTextColor = ContextCompat .getColorStateList(context, R.color.dialog_text_color_holo_dark);
            //getResources().getColorStateList(R.color.dialog_text_color_holo_dark);
}
 
Example 13
Source File: NumberPickerDialogFragment.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle args = getArguments();
    if (args != null && args.containsKey(REFERENCE_KEY)) {
        mReference = args.getInt(REFERENCE_KEY);
    }
    if (args != null && args.containsKey(THEME_RES_ID_KEY)) {
        mTheme = args.getInt(THEME_RES_ID_KEY);
    }
    if (args != null && args.containsKey(PLUS_MINUS_VISIBILITY_KEY)) {
        mPlusMinusVisibility = args.getInt(PLUS_MINUS_VISIBILITY_KEY);
    }
    if (args != null && args.containsKey(DECIMAL_VISIBILITY_KEY)) {
        mDecimalVisibility = args.getInt(DECIMAL_VISIBILITY_KEY);
    }
    if (args != null && args.containsKey(MIN_NUMBER_KEY)) {
        mMinNumber = (BigDecimal) args.getSerializable(MIN_NUMBER_KEY);
    }
    if (args != null && args.containsKey(MAX_NUMBER_KEY)) {
        mMaxNumber = (BigDecimal) args.getSerializable(MAX_NUMBER_KEY);
    }
    if (args != null && args.containsKey(LABEL_TEXT_KEY)) {
        mLabelText = args.getString(LABEL_TEXT_KEY);
    }
    if (args != null && args.containsKey(CURRENT_NUMBER_KEY)) {
        mCurrentNumber = args.getInt(CURRENT_NUMBER_KEY);
    }
    if (args != null && args.containsKey(CURRENT_DECIMAL_KEY)) {
        mCurrentDecimal = args.getDouble(CURRENT_DECIMAL_KEY);
    }
    if (args != null && args.containsKey(CURRENT_SIGN_KEY)) {
        mCurrentSign = args.getInt(CURRENT_SIGN_KEY);
    }

    setStyle(DialogFragment.STYLE_NO_TITLE, 0);

    // Init defaults
    if (getActivity() != null)
        mTextColor = ContextCompat .getColorStateList(getActivity(), R.color.dialog_text_color_holo_dark);
                //getResources().getColorStateList(R.color.dialog_text_color_holo_dark);
    mDialogBackgroundResId = R.drawable.dialog_full_holo_dark;

    if (mTheme != -1) {
        if (getActivity() != null) {
            TypedArray a = getActivity().getApplicationContext().obtainStyledAttributes(mTheme, R.styleable.BetterPickersDialogFragment);

            mTextColor = a.getColorStateList(R.styleable.BetterPickersDialogFragment_bpTextColor);
            mDialogBackgroundResId = a.getResourceId(R.styleable.BetterPickersDialogFragment_bpDialogBackground, mDialogBackgroundResId);

            a.recycle();
        }
    }
}