diff --git a/README.md b/README.md index 9d838b5..0956426 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ ![TagsEditText](https://img.shields.io/badge/Android%20Arsenal-TagsEditText-green.svg?style=true%29%5D%28https://android-arsenal.com/details/1/3581) + Welcome to TagEditView! =================== diff --git a/TagsEditText/src/main/AndroidManifest.xml b/TagsEditText/src/main/AndroidManifest.xml index 49bb18e..f6b1ba1 100644 --- a/TagsEditText/src/main/AndroidManifest.xml +++ b/TagsEditText/src/main/AndroidManifest.xml @@ -3,8 +3,7 @@ + android:supportsRtl="true" > diff --git a/TagsEditText/src/main/java/mabbas007/tagsedittext/TagsEditText.java b/TagsEditText/src/main/java/mabbas007/tagsedittext/TagsEditText.java index 65db3c7..067aed9 100644 --- a/TagsEditText/src/main/java/mabbas007/tagsedittext/TagsEditText.java +++ b/TagsEditText/src/main/java/mabbas007/tagsedittext/TagsEditText.java @@ -1,6 +1,5 @@ package mabbas007.tagsedittext; -import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; @@ -12,12 +11,12 @@ import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; -import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; import android.support.annotation.DimenRes; import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; +import android.support.v7.widget.AppCompatAutoCompleteTextView; import android.text.Editable; import android.text.InputType; import android.text.Spannable; @@ -31,7 +30,6 @@ import android.util.TypedValue; import android.view.View; import android.view.ViewTreeObserver; -import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.TextView; @@ -48,7 +46,7 @@ * Needs a lot of work * BETA */ -public class TagsEditText extends AutoCompleteTextView { +public class TagsEditText extends AppCompatAutoCompleteTextView { public static final String NEW_LINE = "\n"; @@ -68,16 +66,17 @@ public class TagsEditText extends AutoCompleteTextView { private String mSeparator = " "; private String mLastString = ""; private boolean mIsAfterTextWatcherEnabled = true; + private int mTagsLimit = 0; private int mTagsTextColor; private float mTagsTextSize; private Drawable mTagsBackground; private int mTagsBackgroundResource = 0; private Drawable mLeftDrawable; - private int mLeftDrawableResouce = 0; + private int mLeftDrawableResource = 0; private Drawable mRightDrawable; - private int mRightDrawableResouce = 0; + private int mRightDrawableResource = 0; private int mDrawablePadding; @@ -94,6 +93,7 @@ public class TagsEditText extends AutoCompleteTextView { private TagsEditListener mListener; + private final TextWatcher mTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -119,6 +119,7 @@ public void setSeparator(String separator) { mSeparator = separator; } + public TagsEditText(Context context) { super(context); init(null, 0, 0); @@ -134,11 +135,6 @@ public TagsEditText(Context context, AttributeSet attrs, int defStyleAttr) { init(attrs, defStyleAttr, 0); } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public TagsEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - init(attrs, defStyleAttr, defStyleRes); - } @Override protected void onSelectionChanged(int selStart, int selEnd) { @@ -212,7 +208,6 @@ public void setTags(CharSequence... tags) { /** * use this method to set tags */ - public void setTags(String[] tags) { mTagSpans.clear(); mTags.clear(); @@ -249,8 +244,8 @@ public Parcelable onSaveInstanceState() { bundle.putInt(TAGS_TEXT_COLOR, mTagsTextColor); bundle.putInt(TAGS_BACKGROUND_RESOURCE, mTagsBackgroundResource); bundle.putFloat(TAGS_TEXT_SIZE, mTagsTextSize); - bundle.putInt(LEFT_DRAWABLE_RESOURCE, mLeftDrawableResouce); - bundle.putInt(RIGHT_DRAWABLE_RESOURCE, mRightDrawableResouce); + bundle.putInt(LEFT_DRAWABLE_RESOURCE, mLeftDrawableResource); + bundle.putInt(RIGHT_DRAWABLE_RESOURCE, mRightDrawableResource); bundle.putInt(DRAWABLE_PADDING, mDrawablePadding); bundle.putBoolean(ALLOW_SPACES_IN_TAGS, mIsSpacesAllowedInTags); @@ -273,14 +268,14 @@ public void onRestoreInstanceState(Parcelable state) { mTagsTextSize = bundle.getFloat(TAGS_TEXT_SIZE, mTagsTextSize); - mLeftDrawableResouce = bundle.getInt(LEFT_DRAWABLE_RESOURCE, mLeftDrawableResouce); - if (mLeftDrawableResouce != 0) { - mLeftDrawable = ContextCompat.getDrawable(context, mLeftDrawableResouce); + mLeftDrawableResource = bundle.getInt(LEFT_DRAWABLE_RESOURCE, mLeftDrawableResource); + if (mLeftDrawableResource != 0) { + mLeftDrawable = ContextCompat.getDrawable(context, mLeftDrawableResource); } - mRightDrawableResouce = bundle.getInt(RIGHT_DRAWABLE_RESOURCE, mRightDrawableResouce); - if (mRightDrawableResouce != 0) { - mRightDrawable = ContextCompat.getDrawable(context, mRightDrawableResouce); + mRightDrawableResource = bundle.getInt(RIGHT_DRAWABLE_RESOURCE, mRightDrawableResource); + if (mRightDrawableResource != 0) { + mRightDrawable = ContextCompat.getDrawable(context, mRightDrawableResource); } mDrawablePadding = bundle.getInt(DRAWABLE_PADDING, mDrawablePadding); @@ -309,6 +304,7 @@ public void onRestoreInstanceState(Parcelable state) { } } + private void buildStringWithTags(List tags) { mIsAfterTextWatcherEnabled = false; getText().clear(); @@ -323,7 +319,7 @@ private void buildStringWithTags(List tags) { } public void setTagsTextColor(@ColorRes int color) { - mTagsTextColor = getColor(getContext(), color); + mTagsTextColor = ContextCompat.getColor(getContext(), color); setTags(convertTagSpanToArray(mTagSpans)); } @@ -340,13 +336,13 @@ public void setTagsBackground(@DrawableRes int drawable) { public void setCloseDrawableLeft(@DrawableRes int drawable) { mLeftDrawable = ContextCompat.getDrawable(getContext(), drawable); - mLeftDrawableResouce = drawable; + mLeftDrawableResource = drawable; setTags(convertTagSpanToArray(mTagSpans)); } public void setCloseDrawableRight(@DrawableRes int drawable) { mRightDrawable = ContextCompat.getDrawable(getContext(), drawable); - mRightDrawableResouce = drawable; + mRightDrawableResource = drawable; setTags(convertTagSpanToArray(mTagSpans)); } @@ -364,20 +360,16 @@ public void setTagsListener(TagsEditListener listener) { mListener = listener; } - @ColorInt - private int getColor(Context context, @ColorRes int colorId) { - if (Build.VERSION.SDK_INT >= 23) { - return ContextCompat.getColor(context, colorId); - } else { - return context.getResources().getColor(colorId); - } + public void setTagsLimit(int limit) { + this.mTagsLimit = limit; } + private void init(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { Context context = getContext(); if (attrs == null) { mIsSpacesAllowedInTags = false; - mTagsTextColor = getColor(context, R.color.defaultTagsTextColor); + mTagsTextColor = ContextCompat.getColor(context, R.color.defaultTagsTextColor); mTagsTextSize = ResourceUtils.getDimensionPixelSize(context, R.dimen.defaultTagsTextSize); mTagsBackground = ContextCompat.getDrawable(context, R.drawable.oval); mRightDrawable = ContextCompat.getDrawable(context, R.drawable.tag_close); @@ -391,7 +383,7 @@ private void init(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRe try { mIsSpacesAllowedInTags = typedArray.getBoolean(R.styleable.TagsEditText_allowSpaceInTag, false); mTagsTextColor = typedArray.getColor(R.styleable.TagsEditText_tagsTextColor, - getColor(context, R.color.defaultTagsTextColor)); + ContextCompat.getColor(context, R.color.defaultTagsTextColor)); mTagsTextSize = typedArray.getDimensionPixelSize(R.styleable.TagsEditText_tagsTextSize, ResourceUtils.getDimensionPixelSize(context, R.dimen.defaultTagsTextSize)); mTagsBackground = typedArray.getDrawable(R.styleable.TagsEditText_tagsBackground); @@ -438,7 +430,7 @@ private void setTags() { mIsAfterTextWatcherEnabled = false; boolean isEnterClicked = false; - final Editable editable = getText(); + Editable editable = getText(); String str = editable.toString(); if (str.endsWith(NEW_LINE)) { isEnterClicked = true; @@ -457,8 +449,9 @@ private void setTags() { } } - if (getFilter() != null) { - performFiltering(getNewTag(str), 0); + String tempTag = getNewTag(str); + if (getFilter() != null && tempTag != null ) { + performFiltering(tempTag, 0); } if (str.endsWith(NEW_LINE) || (!mIsSpacesAllowedInTags && str.endsWith(mSeparator)) && !isDeleting) { @@ -534,6 +527,11 @@ private void updateTags(String newString) { } private String getNewTag(String newString) { + + if ( mTagsLimit > 0 && mTags.size() >= mTagsLimit ) { + return null; + } + StringBuilder builder = new StringBuilder(); for (Tag tag : mTags) { if (!tag.isSpan()) continue; diff --git a/TagsEditText/src/main/java/mabbas007/tagsedittext/utils/ResourceUtils.java b/TagsEditText/src/main/java/mabbas007/tagsedittext/utils/ResourceUtils.java index e9bc116..3c2c75c 100644 --- a/TagsEditText/src/main/java/mabbas007/tagsedittext/utils/ResourceUtils.java +++ b/TagsEditText/src/main/java/mabbas007/tagsedittext/utils/ResourceUtils.java @@ -1,11 +1,7 @@ package mabbas007.tagsedittext.utils; import android.content.Context; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.support.annotation.ColorRes; import android.support.annotation.DimenRes; -import android.support.annotation.DrawableRes; /** * Created by Mohammad Abbas on 5/10/2016. diff --git a/app/src/main/java/mabbas007/myapplication/MainActivity.java b/app/src/main/java/mabbas007/myapplication/MainActivity.java index 0b371c9..0fcaef7 100644 --- a/app/src/main/java/mabbas007/myapplication/MainActivity.java +++ b/app/src/main/java/mabbas007/myapplication/MainActivity.java @@ -37,6 +37,8 @@ protected void onCreate(Bundle savedInstanceState) { setButtonClickListener(R.id.btnChangeDrawableLeft); setButtonClickListener(R.id.btnChangeDrawableRight); setButtonClickListener(R.id.btnChangeClosePadding); + setButtonClickListener(R.id.btnSetLimit); + } @Override @@ -49,6 +51,8 @@ public void onWindowFocusChanged(boolean hasFocus) { @Override public void onClick(View v) { + mTagsEditText.setTagsLimit(0); + switch (v.getId()) { case R.id.btnChangeTags: { //mTagsEditText.setTags("1", "2", "3"); @@ -72,13 +76,21 @@ public void onClick(View v) { break; } case R.id.btnChangeDrawableRight: { - mTagsEditText.setCloseDrawableRight(R.drawable.tag_close); + mTagsEditText.setCloseDrawableRight(R.drawable.dot); break; } case R.id.btnChangeClosePadding: { mTagsEditText.setCloseDrawablePadding(R.dimen.larger_padding); break; } + + case R.id.btnSetLimit: + mTagsEditText.setTags(new String[]{"A","B","C","D","5"}); + mTagsEditText.setTagsLimit(5); + break; + + default: + break; } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 09c1a36..4009e77 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -72,6 +72,14 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change Close Padding" /> + + +