From e420c37a0e4efc8e71af186d67925d47ecbdfb4b Mon Sep 17 00:00:00 2001 From: markwei Date: Wed, 25 Oct 2017 03:17:36 -0400 Subject: [PATCH] Create ChipDrawable from XML resource. Add demo of using ChipDrawable in EditText. Set the initial state of the chip and close icon to state_enabled. This is not done automatically if using ChipDrawable in a span. PiperOrigin-RevId: 173362627 --- lib/src/android/support/design/chip/Chip.java | 4 +- .../support/design/chip/ChipDrawable.java | 55 ++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/src/android/support/design/chip/Chip.java b/lib/src/android/support/design/chip/Chip.java index 1e0843e1e..efa938a5d 100644 --- a/lib/src/android/support/design/chip/Chip.java +++ b/lib/src/android/support/design/chip/Chip.java @@ -62,8 +62,8 @@ import java.util.List; * or {@link #setOnCheckedChangeListener(OnCheckedChangeListener)}. You can register a listener on * the close icon with {@link #setOnCloseIconClickListener(OnClickListener)}. * - *

Do not use the {@code android:button} attribute. It will be ignored because Chip manages its - * own button Drawable. + *

All attributes from {@link R.styleable#ChipDrawable} are supported. Do not use the {@code + * android:button} attribute. It will be ignored because Chip manages its own button Drawable. * * @see ChipDrawable */ diff --git a/lib/src/android/support/design/chip/ChipDrawable.java b/lib/src/android/support/design/chip/ChipDrawable.java index 24dc54384..243c216aa 100644 --- a/lib/src/android/support/design/chip/ChipDrawable.java +++ b/lib/src/android/support/design/chip/ChipDrawable.java @@ -18,6 +18,8 @@ package android.support.design.chip; import android.content.Context; import android.content.res.ColorStateList; +import android.content.res.Resources; +import android.content.res.Resources.NotFoundException; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.ColorFilter; @@ -43,6 +45,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.annotation.StyleRes; +import android.support.annotation.XmlRes; import android.support.design.animation.MotionSpec; import android.support.design.canvas.CanvasCompat; import android.support.design.drawable.DrawableUtils; @@ -52,11 +55,15 @@ import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v4.graphics.drawable.TintAwareDrawable; import android.support.v7.content.res.AppCompatResources; import android.text.TextPaint; +import android.text.TextUtils; import android.text.style.TextAppearanceSpan; import android.util.AttributeSet; -import android.util.StateSet; +import android.util.Xml; import android.view.View; +import java.io.IOException; import java.util.Arrays; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; /** * ChipDrawable contains all the layout and draw logic for {@link Chip}. @@ -103,6 +110,7 @@ import java.util.Arrays; public class ChipDrawable extends Drawable implements TintAwareDrawable, Callback { private static final boolean DEBUG = false; + private static final int[] DEFAULT_STATE = new int[] {android.R.attr.state_enabled}; // Visuals @Nullable private ColorStateList chipBackgroundColor; @@ -184,7 +192,7 @@ public class ChipDrawable extends Drawable implements TintAwareDrawable, Callbac @Nullable private PorterDuffColorFilter tintFilter; @Nullable private ColorStateList tint; @Nullable private Mode tintMode = Mode.SRC_IN; - private int[] closeIconStateSet = StateSet.WILD_CARD; + private int[] closeIconStateSet; /** Returns a ChipDrawable from the given attributes. */ public static ChipDrawable createFromAttributes( @@ -194,11 +202,54 @@ public class ChipDrawable extends Drawable implements TintAwareDrawable, Callbac return chip; } + /** + * Returns a ChipDrawable from the given XML resource. All attributes from {@link + * R.styleable#ChipDrawable} and a custom style attribute are supported. A chip + * resource may look like: + * + *

{@code
+   * 
+   * }
+ */ + public static ChipDrawable createFromResource(Context context, @XmlRes int id) { + try { + XmlPullParser parser = context.getResources().getXml(id); + + int type; + do { + type = parser.next(); + } while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT); + if (type != XmlPullParser.START_TAG) { + throw new XmlPullParserException("No start tag found"); + } + + if (!TextUtils.equals(parser.getName(), "chip")) { + throw new XmlPullParserException("Must have a start tag"); + } + + AttributeSet attrs = Xml.asAttributeSet(parser); + int style = attrs.getStyleAttribute(); + + return createFromAttributes(context, attrs, R.attr.chipStyle, style); + } catch (XmlPullParserException | IOException e) { + Resources.NotFoundException exception = + new NotFoundException("Can't load chip resource ID #0x" + Integer.toHexString(id)); + exception.initCause(e); + throw exception; + } + } + private ChipDrawable(Context context) { this.context = context; textPaint.density = context.getResources().getDisplayMetrics().density; debugPaint = DEBUG ? new Paint(Paint.ANTI_ALIAS_FLAG) : null; + + setState(DEFAULT_STATE); + setCloseIconState(DEFAULT_STATE); } private void loadFromAttributes(