Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<a href="https://android-arsenal.com/details/1/3581/" rel="Android Arsenal TagsEditText">![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!
===================

Expand Down
3 changes: 1 addition & 2 deletions TagsEditText/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

<application android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>
android:supportsRtl="true" >

</application>

Expand Down
68 changes: 33 additions & 35 deletions TagsEditText/src/main/java/mabbas007/tagsedittext/TagsEditText.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mabbas007.tagsedittext;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
Expand All @@ -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;
Expand All @@ -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;

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

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

Expand All @@ -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) {
Expand All @@ -119,6 +119,7 @@ public void setSeparator(String separator) {
mSeparator = separator;
}


public TagsEditText(Context context) {
super(context);
init(null, 0, 0);
Expand All @@ -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) {
Expand Down Expand Up @@ -212,7 +208,6 @@ public void setTags(CharSequence... tags) {
/**
* use this method to set tags
*/

public void setTags(String[] tags) {
mTagSpans.clear();
mTags.clear();
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -309,6 +304,7 @@ public void onRestoreInstanceState(Parcelable state) {
}
}


private void buildStringWithTags(List<Tag> tags) {
mIsAfterTextWatcherEnabled = false;
getText().clear();
Expand All @@ -323,7 +319,7 @@ private void buildStringWithTags(List<Tag> tags) {
}

public void setTagsTextColor(@ColorRes int color) {
mTagsTextColor = getColor(getContext(), color);
mTagsTextColor = ContextCompat.getColor(getContext(), color);
setTags(convertTagSpanToArray(mTagSpans));
}

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

Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/mabbas007/myapplication/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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;
}
}

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Close Padding" />


<Button
android:id="@+id/btnSetLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Limit Of 5 Tags" />

</LinearLayout>

</ScrollView>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed May 10 22:28:01 GMT+02:00 2017
#Sat May 20 16:35:30 VET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
Loading