diff --git a/CHANGELOG.md b/CHANGELOG.md index 780f9260..be42719b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +# 1.0.2 + +Fixed bug with avatar icon when creating `ChipView` programmatically ([see](https://github.com/pchmn/MaterialChipsInput/issues/2) issue) + # 1.0.1 Fixed bug with `android.view.ContextThemeWrapper cannot be cast to android.app.Activity` ([see](https://github.com/pchmn/MaterialChipsInput/issues/1) issue) diff --git a/README.md b/README.md index 32968bd7..10a7c5c9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Implementation of Material Design [Chips](https://material.io/guidelines/compone Demo ## Demo -[Download sample-v1.0.1.apk](https://github.com/pchmn/MaterialChipsInput/raw/master/docs/material-chips-input-sample-v1.0.1.apk) +[Download sample-v1.0.2.apk](https://github.com/pchmn/MaterialChipsInput/raw/master/docs/material-chips-input-sample-v1.0.2.apk) ## Setup @@ -26,7 +26,7 @@ allprojects { In your app level build.gradle : ```java dependencies { - compile 'com.github.pchmn:MaterialChipsInput:1.0.1' + compile 'com.github.pchmn:MaterialChipsInput:1.0.2' } ```

diff --git a/docs/material-chips-input-sample-v1.0.2.apk b/docs/material-chips-input-sample-v1.0.2.apk new file mode 100644 index 00000000..7ec73349 Binary files /dev/null and b/docs/material-chips-input-sample-v1.0.2.apk differ diff --git a/library/build.gradle b/library/build.gradle index ba4cbcb2..3f5d3941 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 25 - versionCode 2 - versionName "1.0.1" + versionCode 3 + versionName "1.0.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/library/src/main/java/com/pchmn/materialchips/ChipView.java b/library/src/main/java/com/pchmn/materialchips/ChipView.java index 6c6b27bd..4db85788 100644 --- a/library/src/main/java/com/pchmn/materialchips/ChipView.java +++ b/library/src/main/java/com/pchmn/materialchips/ChipView.java @@ -120,19 +120,9 @@ private void inflateWithAttributes() { // avatar setHasAvatarIcon(mHasAvatarIcon); - if(mAvatarIconUri != null) - setAvatarIcon(mAvatarIconUri); - else if(mAvatarIconDrawable != null) - setAvatarIcon(mAvatarIconDrawable); - else - mAvatarIconImageView.setImageBitmap(mLetterTileProvider.getLetterTile(getLabel())); // delete button setDeletable(mDeletable); - if(mDeleteIcon != null) - setDeleteIcon(mDeleteIcon); - if(mDeleteIconColor != null) - setDeleteIconColor(mDeleteIconColor); // background color if(mBackgroundColor != null) @@ -160,7 +150,7 @@ else if(mHasAvatarIcon) * @return the label */ public String getLabel() { - return mLabelTextView.getText().toString(); + return mLabel; } /** @@ -169,6 +159,7 @@ public String getLabel() { * @param label the label to set */ public void setLabel(String label) { + mLabel = label; mLabelTextView.setText(label); } @@ -178,6 +169,7 @@ public void setLabel(String label) { * @param color the color to set */ public void setLabelColor(ColorStateList color) { + mLabelColor = color; mLabelTextView.setTextColor(color); } @@ -187,6 +179,7 @@ public void setLabelColor(ColorStateList color) { * @param color the color to set */ public void setLabelColor(@ColorInt int color) { + mLabelColor = ColorStateList.valueOf(color); mLabelTextView.setTextColor(color); } @@ -196,7 +189,9 @@ public void setLabelColor(@ColorInt int color) { * @param hasAvatarIcon true to show, false to hide */ public void setHasAvatarIcon(boolean hasAvatarIcon) { - if(!hasAvatarIcon) { + mHasAvatarIcon = hasAvatarIcon; + + if(!mHasAvatarIcon) { // hide icon mAvatarIconImageView.setVisibility(GONE); // adjust padding @@ -214,6 +209,14 @@ public void setHasAvatarIcon(boolean hasAvatarIcon) { mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, 0, 0); else mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, ViewUtil.dpToPx(12), 0); + + // set icon + if(mAvatarIconUri != null) + mAvatarIconImageView.setImageURI(mAvatarIconUri); + else if(mAvatarIconDrawable != null) + mAvatarIconImageView.setImageDrawable(mAvatarIconDrawable); + else + mAvatarIconImageView.setImageBitmap(mLetterTileProvider.getLetterTile(getLabel())); } } @@ -223,16 +226,9 @@ public void setHasAvatarIcon(boolean hasAvatarIcon) { * @param avatarIcon the icon to set */ public void setAvatarIcon(Drawable avatarIcon) { - // set icon - mAvatarIconImageView.setImageDrawable(avatarIcon); - - // show icon - mAvatarIconImageView.setVisibility(VISIBLE); - // adjust padding - if(mDeleteButton.getVisibility() == VISIBLE) - mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, 0, 0); - else - mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, ViewUtil.dpToPx(12), 0); + mAvatarIconDrawable = avatarIcon; + mHasAvatarIcon = true; + inflateWithAttributes(); } /** @@ -241,16 +237,9 @@ public void setAvatarIcon(Drawable avatarIcon) { * @param avatarUri the uri of the icon to set */ public void setAvatarIcon(Uri avatarUri) { - // set icon - mAvatarIconImageView.setImageURI(avatarUri); - - // show icon - mAvatarIconImageView.setVisibility(VISIBLE); - // adjust padding - if(mDeleteButton.getVisibility() == VISIBLE) - mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, 0, 0); - else - mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, ViewUtil.dpToPx(12), 0); + mAvatarIconUri = avatarUri; + mHasAvatarIcon = true; + inflateWithAttributes(); } /** @@ -259,7 +248,8 @@ public void setAvatarIcon(Uri avatarUri) { * @param deletable true to show, false to hide */ public void setDeletable(boolean deletable) { - if(!deletable) { + mDeletable = deletable; + if(!mDeletable) { // hide delete icon mDeleteButton.setVisibility(GONE); // adjust padding @@ -276,6 +266,12 @@ public void setDeletable(boolean deletable) { mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, 0, 0); else mLabelTextView.setPadding(ViewUtil.dpToPx(12), 0, 0, 0); + + // set icon + if(mDeleteIcon != null) + mDeleteButton.setImageDrawable(mDeleteIcon); + if(mDeleteIconColor != null) + mDeleteButton.getDrawable().mutate().setColorFilter(mDeleteIconColor.getDefaultColor(), PorterDuff.Mode.SRC_ATOP); } } @@ -285,7 +281,9 @@ public void setDeletable(boolean deletable) { * @param color the color to set */ public void setDeleteIconColor(ColorStateList color) { - setDeleteIconColor(color.getDefaultColor()); + mDeleteIconColor = color; + mDeletable = true; + inflateWithAttributes(); } /** @@ -294,16 +292,9 @@ public void setDeleteIconColor(ColorStateList color) { * @param color the color to set */ public void setDeleteIconColor(@ColorInt int color) { - // set color - mDeleteButton.getDrawable().mutate().setColorFilter(color, PorterDuff.Mode.SRC_ATOP); - - // show icon - mDeleteButton.setVisibility(VISIBLE); - // adjust padding - if(mAvatarIconImageView.getVisibility() == VISIBLE) - mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, 0, 0); - else - mLabelTextView.setPadding(ViewUtil.dpToPx(12), 0, 0, 0); + mDeleteIconColor = ColorStateList.valueOf(color); + mDeletable = true; + inflateWithAttributes(); } /** @@ -312,16 +303,9 @@ public void setDeleteIconColor(@ColorInt int color) { * @param deleteIcon the icon to set */ public void setDeleteIcon(Drawable deleteIcon) { - // set icon - mDeleteButton.setImageDrawable(deleteIcon); - - // show icon - mDeleteButton.setVisibility(VISIBLE); - // adjust padding - if(mAvatarIconImageView.getVisibility() == VISIBLE) - mLabelTextView.setPadding(ViewUtil.dpToPx(8), 0, 0, 0); - else - mLabelTextView.setPadding(ViewUtil.dpToPx(12), 0, 0, 0); + mDeleteIcon = deleteIcon; + mDeletable = true; + inflateWithAttributes(); } /** @@ -330,6 +314,7 @@ public void setDeleteIcon(Drawable deleteIcon) { * @param color the color to set */ public void setChipBackgroundColor(ColorStateList color) { + mBackgroundColor = color; setChipBackgroundColor(color.getDefaultColor()); } @@ -339,6 +324,7 @@ public void setChipBackgroundColor(ColorStateList color) { * @param color the color to set */ public void setChipBackgroundColor(@ColorInt int color) { + mBackgroundColor = ColorStateList.valueOf(color); mContentLayout.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP); } diff --git a/sample/build.gradle b/sample/build.gradle index 2157d6d0..01dde3f4 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.pchmn.sample.materialchipsinput" minSdkVersion 15 targetSdkVersion 25 - versionCode 2 - versionName "1.0.1" + versionCode 3 + versionName "1.0.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/sample/src/main/java/com/pchmn/sample/materialchipsinput/ChipExamplesActivity.java b/sample/src/main/java/com/pchmn/sample/materialchipsinput/ChipExamplesActivity.java index 8ba18c73..33a11121 100644 --- a/sample/src/main/java/com/pchmn/sample/materialchipsinput/ChipExamplesActivity.java +++ b/sample/src/main/java/com/pchmn/sample/materialchipsinput/ChipExamplesActivity.java @@ -1,8 +1,11 @@ package com.pchmn.sample.materialchipsinput; +import android.net.Uri; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; +import android.widget.LinearLayout; import android.widget.Toast; import com.pchmn.materialchips.ChipView; @@ -13,6 +16,7 @@ public class ChipExamplesActivity extends AppCompatActivity { private static final String TAG = ChipExamplesActivity.class.toString(); + @BindView(R.id.layout) LinearLayout mLayout; @BindView(R.id.chip1) ChipView mChip1; @BindView(R.id.chip2) ChipView mChip2; @BindView(R.id.chip3) ChipView mChip3; @@ -77,5 +81,24 @@ protected void onCreate(Bundle savedInstanceState) { mChip7.setOnDeleteClicked(view -> { Toast.makeText(ChipExamplesActivity.this, mChip7.getLabel() + ": delete clicked", Toast.LENGTH_SHORT).show(); }); + + + // programmatically + Uri uri = null; + ChipView chipView1 = new ChipView(this); + chipView1.setLabel("Test 1"); + chipView1.setLabelColor(ContextCompat.getColor(this, R.color.colorPrimary)); + chipView1.setAvatarIcon(uri); + chipView1.setHasAvatarIcon(true); + + ChipView chipView2 = new ChipView(this); + chipView2.setLabel("Test 1"); + chipView2.setChipBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)); + chipView2.setLabelColor(ContextCompat.getColor(this, R.color.colorPrimary)); + chipView2.setAvatarIcon(uri); + chipView2.setDeleteIconColor(ContextCompat.getColor(this, R.color.colorPrimary)); + + //mLayout.addView(chipView1); + //mLayout.addView(chipView2); } } diff --git a/sample/src/main/res/layout/activity_chip_examples.xml b/sample/src/main/res/layout/activity_chip_examples.xml index 68442c51..5d106b14 100644 --- a/sample/src/main/res/layout/activity_chip_examples.xml +++ b/sample/src/main/res/layout/activity_chip_examples.xml @@ -7,6 +7,7 @@ tools:context="com.pchmn.sample.materialchipsinput.MainActivity">