List of usage examples for android.content.res ColorStateList ColorStateList
public ColorStateList(int[][] states, @ColorInt int[] colors)
From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java
/** * Fill in this object based on the contents of an XML "selector" element. *///from w w w.j a v a 2 s . c o m private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException { final int innerDepth = parser.getDepth() + 1; int depth; int type; int defaultColor = DEFAULT_COLOR; int[][] stateSpecList = new int[20][]; int[] colorList = new int[stateSpecList.length]; int listSize = 0; while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) { if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) { continue; } final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem); final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA); float alphaMod = 1.0f; if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) { alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod); } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) { alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod); } a.recycle(); // Parse all unrecognized attributes as state specifiers. int j = 0; final int numAttrs = attrs.getAttributeCount(); int[] stateSpec = new int[numAttrs]; for (int i = 0; i < numAttrs; i++) { final int stateResId = attrs.getAttributeNameResource(i); if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha && stateResId != R.attr.alpha) { // Unrecognized attribute, add to state set stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId; } } stateSpec = StateSet.trimStateSet(stateSpec, j); // Apply alpha modulation. If we couldn't resolve the color or // alpha yet, the default values leave us enough information to // modulate again during applyTheme(). final int color = modulateColorAlpha(baseColor, alphaMod); if (listSize == 0 || stateSpec.length == 0) { defaultColor = color; } colorList = GrowingArrayUtils.append(colorList, listSize, color); stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec); listSize++; } int[] colors = new int[listSize]; int[][] stateSpecs = new int[listSize][]; System.arraycopy(colorList, 0, colors, 0, listSize); System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize); return new ColorStateList(stateSpecs, colors); }
From source file:com.philliphsu.clock2.alarms.ui.ExpandedAlarmViewHolder.java
public ExpandedAlarmViewHolder(ViewGroup parent, final OnListItemInteractionListener<Alarm> listener, AlarmController controller) {/*from w w w . java 2s . co m*/ super(parent, R.layout.item_expanded_alarm, listener, controller); // Manually bind listeners, or else you'd need to write a getter for the // OnListItemInteractionListener in the BaseViewHolder for use in method binding. mDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onListItemDeleted(getAlarm()); } }); mOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Since changes are persisted as soon as they are made, there's really // nothing we have to persist here. Let the listener know we should // collapse this VH. // While this works, it also makes an update to the DB and thus reschedules // the alarm, so the snackbar will show up as well. We want to avoid that.. // listener.onListItemUpdate(getAlarm(), getAdapterPosition()); // TODO: This only works because we know what the implementation looks like.. // This is bad because we just made the proper function of this dependent // on the implementation. listener.onListItemClick(getAlarm(), getAdapterPosition()); } }); // https://code.google.com/p/android/issues/detail?id=177282 // https://stackoverflow.com/questions/15673449/is-it-confirmed-that-i-cannot-use-themed-color-attribute-in-color-state-list-res // Programmatically create the ColorStateList for our day toggles using themed color // attributes, "since prior to M you can't create a themed ColorStateList from XML but you // can from code." (quote from google) // The first array level is analogous to an XML node defining an item with a state list. // The second level lists all the states considered by the item from the first level. // An empty list of states represents the default stateless item. int[][] states = { /*item 1*/{ /*states*/android.R.attr.state_checked }, /*item 2*/{ /*states*/ } }; // TODO: Phase out Utils.getColorFromThemeAttr because it doesn't work for text colors. // WHereas getTextColorFromThemeAttr works for both regular colors and text colors. int[] dayToggleColors = { /*item 1*/Utils.getTextColorFromThemeAttr(getContext(), R.attr.colorAccent), /*item 2*/Utils.getTextColorFromThemeAttr(getContext(), android.R.attr.textColorHint) }; int[] vibrateColors = { /*item 1*/Utils.getTextColorFromThemeAttr(getContext(), R.attr.colorAccent), /*item 2*/Utils.getTextColorFromThemeAttr(getContext(), R.attr.themedIconTint) }; mDayToggleColors = new ColorStateList(states, dayToggleColors); mVibrateColors = new ColorStateList(states, vibrateColors); mRingtonePickerController = new RingtonePickerDialogController(mFragmentManager, new RingtonePickerDialog.OnRingtoneSelectedListener() { @Override public void onRingtoneSelected(Uri ringtoneUri) { Log.d(TAG, "Selected ringtone: " + ringtoneUri.toString()); final Alarm oldAlarm = getAlarm(); Alarm newAlarm = oldAlarm.toBuilder().ringtone(ringtoneUri.toString()).build(); oldAlarm.copyMutableFieldsTo(newAlarm); persistUpdatedAlarm(newAlarm, false); } }); }
From source file:com.gudong.appkit.ui.fragment.ColorChooseDialog.java
/** * View//from w ww .j a v a 2 s .c o m * * @param view * @param color */ private void setBackgroundSelector(View view, int color) { Drawable selector = createSelector(color); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { int[][] states = new int[][] { new int[] { -android.R.attr.state_pressed }, new int[] { android.R.attr.state_pressed } }; int[] colors = new int[] { shiftColor(color), color }; ColorStateList rippleColors = new ColorStateList(states, colors); setBackgroundCompat(view, new RippleDrawable(rippleColors, selector, null)); } else { setBackgroundCompat(view, selector); } }
From source file:com.finchuk.clock2.alarms.ui.ExpandedAlarmViewHolder.java
public ExpandedAlarmViewHolder(ViewGroup parent, final OnListItemInteractionListener<Alarm> listener, AlarmController controller) {//from w ww.ja va 2s . co m super(parent, com.finchuk.clock2.R.layout.item_expanded_alarm, listener, controller); // Manually bind listeners, or else you'd need to write a getter for the // OnListItemInteractionListener in the BaseViewHolder for use in method binding. mDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onListItemDeleted(getAlarm()); } }); mOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Since changes are persisted as soon as they are made, there's really // nothing we have to persist here. Let the listener know we should // collapse this VH. // While this works, it also makes an update to the DB and thus reschedules // the alarm, so the snackbar will show up as well. We want to avoid that.. // listener.onListItemUpdate(getAlarm(), getAdapterPosition()); // TODO: This only works because we know what the implementation looks like.. // This is bad because we just made the proper function of this dependent // on the implementation. listener.onListItemClick(getAlarm(), getAdapterPosition()); } }); // https://code.google.com/p/android/issues/detail?id=177282 // https://stackoverflow.com/questions/15673449/is-it-confirmed-that-i-cannot-use-themed-color-attribute-in-color-state-list-res // Programmatically create the ColorStateList for our day toggles using themed color // attributes, "since prior to M you can't create a themed ColorStateList from XML but you // can from code." (quote from google) // The first array level is analogous to an XML node defining an item with a state list. // The second level lists all the states considered by the item from the first level. // An empty list of states represents the default stateless item. int[][] states = { /*item 1*/{ /*states*/android.R.attr.state_checked }, /*item 2*/{ /*states*/ } }; // TODO: Phase out Utils.getColorFromThemeAttr because it doesn't work for text colors. // WHereas getTextColorFromThemeAttr works for both regular colors and text colors. int[] dayToggleColors = { /*item 1*/Utils.getTextColorFromThemeAttr(getContext(), com.finchuk.clock2.R.attr.colorAccent), /*item 2*/Utils.getTextColorFromThemeAttr(getContext(), android.R.attr.textColorHint) }; int[] vibrateColors = { /*item 1*/Utils.getTextColorFromThemeAttr(getContext(), com.finchuk.clock2.R.attr.colorAccent), /*item 2*/Utils.getTextColorFromThemeAttr(getContext(), com.finchuk.clock2.R.attr.themedIconTint) }; mDayToggleColors = new ColorStateList(states, dayToggleColors); mVibrateColors = new ColorStateList(states, vibrateColors); mRingtonePickerController = new RingtonePickerDialogController(mFragmentManager, new RingtonePickerDialog.OnRingtoneSelectedListener() { @Override public void onRingtoneSelected(Uri ringtoneUri) { Log.d(TAG, "Selected ringtone: " + ringtoneUri.toString()); final Alarm oldAlarm = getAlarm(); Alarm newAlarm = oldAlarm.toBuilder().ringtone(ringtoneUri.toString()).build(); oldAlarm.copyMutableFieldsTo(newAlarm); persistUpdatedAlarm(newAlarm, false); } }); }
From source file:me.henrytao.mdcore.core.MdCompat.java
/** * A temporary fix for android.content.res.ColorStateList.inflate *//*from ww w . jav a2 s .c o m*/ public static ColorStateList getColorStateList(Context context, int resId) throws IOException, XmlPullParserException { XmlResourceParser parser = context.getResources().getXml(resId); AttributeSet attrs = Xml.asAttributeSet(parser); Resources r = context.getResources(); final int innerDepth = parser.getDepth() + 2; int depth; int type; List<int[]> customStateList = new ArrayList<>(); List<Integer> customColorList = new ArrayList<>(); while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) { if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) { continue; } // Parse all unrecognized attributes as state specifiers. int j = 0; final int numAttrs = attrs.getAttributeCount(); int color = 0; float alpha = 1.0f; int[] stateSpec = new int[numAttrs]; for (int i = 0; i < numAttrs; i++) { final int stateResId = attrs.getAttributeNameResource(i); switch (stateResId) { case android.R.attr.color: int colorAttrId = attrs.getAttributeResourceValue(i, 0); if (colorAttrId == 0) { String colorAttrValue = attrs.getAttributeValue(i); colorAttrId = Integer.valueOf(colorAttrValue.replace("?", "")); color = getColorFromAttribute(context, colorAttrId); } else { color = ContextCompat.getColor(context, colorAttrId); } break; case android.R.attr.alpha: try { alpha = attrs.getAttributeFloatValue(i, 1.0f); } catch (Exception e) { String alphaAttrValue = attrs.getAttributeValue(i); alpha = getFloatFromAttribute(context, Integer.valueOf(alphaAttrValue.replace("?", ""))); } break; default: stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId; } } stateSpec = StateSet.trimStateSet(stateSpec, j); color = modulateColorAlpha(color, alpha); customColorList.add(color); customStateList.add(stateSpec); } int[] colors = new int[customColorList.size()]; int[][] states = new int[customStateList.size()][]; int i = 0; for (int n = states.length; i < n; i++) { colors[i] = customColorList.get(i); states[i] = customStateList.get(i); } return new ColorStateList(states, colors); }
From source file:com.landenlabs.all_UiDemo.frag.ImageScalesFrag.java
private void setScaledVector(ImageView imageView, int imageRes) { Drawable rawDrawable;/*from w ww .j a v a2 s.com*/ if (Build.VERSION.SDK_INT >= 21) { rawDrawable = getResources().getDrawable(imageRes, getTheme()); } else { rawDrawable = getResources().getDrawable(imageRes); } Drawable drawable = rawDrawable.mutate(); /* int pad = 50; drawable.setBounds(0, pad, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); */ // ScaleDrawable does nothing !!!! if (false) { ScaleDrawable scaleDrawable = new ScaleDrawable(rawDrawable, Gravity.TOP | Gravity.LEFT, 2.0f, 0.8f); scaleDrawable.setLevel(8000); drawable = scaleDrawable.getDrawable(); } if (false) { int tintColor = 0x80ff0000; ColorStateList colorStateList = new ColorStateList(new int[][] { new int[] {} }, new int[] { tintColor }); drawable = DrawableCompat.wrap(drawable.mutate()); DrawableCompat.setTintList(drawable, colorStateList); DrawableCompat.setTintMode(drawable, PorterDuff.Mode.MULTIPLY); } //imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageDrawable(drawable); // imageView.setImageDrawable(null); // imageView.setBackgroundDrawable(drawable); }
From source file:io.mpos.ui.shared.util.UiHelper.java
private static ColorStateList getColorsStateListForTint(int color) { int[][] colorStates = new int[][] { new int[] { -android.R.attr.state_enabled }, // disabled state (-) new int[] {} // enabled };// w w w . j a va2 s. co m int[] colors = new int[] { setAlphaForColor(color, 0.4f), color }; return new ColorStateList(colorStates, colors); }
From source file:br.comoferta.ui.view.SlidingTabLayout.java
/** * Create a default view to be used for tabs. This is called if a custom tab view is not set via * {@link #setCustomTabView(int, int)}./*from ww w. j a va2 s. c om*/ */ protected TextView createDefaultTabView(Context context) { ColorStateList colorStateList = new ColorStateList(new int[][] { new int[] { android.R.attr.state_selected }, new int[] { -android.R.attr.state_selected }, }, new int[] { Color.WHITE, Color.LTGRAY }); TextView textView = new TextView(context); textView.setGravity(Gravity.CENTER); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP); textView.setTextColor(colorStateList); textView.setTypeface(Typeface.DEFAULT_BOLD); textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); TypedValue outValue = new TypedValue(); getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); textView.setBackgroundResource(outValue.resourceId); int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density); textView.setPadding(padding, padding, padding, padding); return textView; }
From source file:de.mrapp.android.preference.ActionPreference.java
/** * Adapts the text color of the preference's title. */// w w w . ja va 2 s . c o m private void adaptTextColor() { if (textView != null) { int[][] states = new int[][] { new int[] { -android.R.attr.state_enabled }, new int[] {} }; int[] colors = new int[] { getDisabledTextColor(), getTextColor() }; ColorStateList colorStateList = new ColorStateList(states, colors); textView.setTextColor(colorStateList); } }
From source file:com.itheima.googlemarket.PagerSlidingTabStrip.java
/** * //from w w w .ja va 2 s. co m * @param selectedColor ?8?16??0x66FF0000 * @param normalColor ?8?16??0x66FF0000 * @return */ private ColorStateList createColorSelector(int selectedColor, int normalColor) { // int[] state1 = { android.R.attr.state_pressed }; // ?? int[] state2 = { android.R.attr.state_selected }; // ?? int[] state3 = {}; // ?? int[][] states = new int[3][1]; states[0] = state1; states[1] = state2; states[2] = state3; int[] colors = { selectedColor, // ? selectedColor, // ? normalColor // ? }; ColorStateList colorStateList = new ColorStateList(states, colors); return colorStateList; }