Skip to content

Commit

Permalink
Fixed bug when creating ChipView programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
pchmn committed Apr 20, 2017
1 parent bd9b78f commit 1336e79
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 60 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Implementation of Material Design [Chips](https://material.io/guidelines/compone
<img src="https://github.com/pchmn/MaterialChipsInput/blob/master/docs/demo2.gif" alt="Demo" height="600px"/>

## 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

Expand All @@ -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'
}
```
<br><br>
Expand Down
Binary file added docs/material-chips-input-sample-v1.0.2.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
94 changes: 40 additions & 54 deletions library/src/main/java/com/pchmn/materialchips/ChipView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -160,7 +150,7 @@ else if(mHasAvatarIcon)
* @return the label
*/
public String getLabel() {
return mLabelTextView.getText().toString();
return mLabel;
}

/**
Expand All @@ -169,6 +159,7 @@ public String getLabel() {
* @param label the label to set
*/
public void setLabel(String label) {
mLabel = label;
mLabelTextView.setText(label);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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
Expand All @@ -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()));
}
}

Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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());
}

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_chip_examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tools:context="com.pchmn.sample.materialchipsinput.MainActivity">

<LinearLayout
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 1336e79

Please sign in to comment.