From 3970e71d193b74b47ae1c5d516e000f400c8fcba Mon Sep 17 00:00:00 2001 From: AkshayMore Date: Wed, 24 May 2017 23:24:18 +0530 Subject: [PATCH 1/2] added functionality to specify max tags in a single line --- library/library.iml | 9 ++--- .../java/com/cunoraz/tagview/Constants.java | 4 +-- .../main/java/com/cunoraz/tagview/Tag.java | 8 +++-- .../java/com/cunoraz/tagview/TagView.java | 34 +++++++++++++++++-- library/src/main/res/values/tagview_attr.xml | 1 + 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/library/library.iml b/library/library.iml index b0c34be..ba885c2 100644 --- a/library/library.iml +++ b/library/library.iml @@ -9,7 +9,6 @@ @@ -48,7 +47,6 @@ - @@ -56,7 +54,6 @@ - @@ -64,7 +61,6 @@ - @@ -72,7 +68,6 @@ - @@ -80,7 +75,6 @@ - @@ -104,5 +98,6 @@ + \ No newline at end of file diff --git a/library/src/main/java/com/cunoraz/tagview/Constants.java b/library/src/main/java/com/cunoraz/tagview/Constants.java index fced029..ab72641 100644 --- a/library/src/main/java/com/cunoraz/tagview/Constants.java +++ b/library/src/main/java/com/cunoraz/tagview/Constants.java @@ -13,9 +13,9 @@ public class Constants { public static final float DEFAULT_TAG_TEXT_PADDING_TOP = 5; public static final float DEFAULT_TAG_TEXT_PADDING_RIGHT = 8; public static final float DEFAULT_TAG_TEXT_PADDING_BOTTOM = 5; - public static final float LAYOUT_WIDTH_OFFSET = 2; - + public static final int DEFAULT_MAX_TAGS = 0; + //----------------- separator Tag Item-----------------// public static final float DEFAULT_TAG_TEXT_SIZE = 14f; public static final float DEFAULT_TAG_DELETE_INDICATOR_SIZE = 14f; diff --git a/library/src/main/java/com/cunoraz/tagview/Tag.java b/library/src/main/java/com/cunoraz/tagview/Tag.java index 14b2727..17a47e5 100644 --- a/library/src/main/java/com/cunoraz/tagview/Tag.java +++ b/library/src/main/java/com/cunoraz/tagview/Tag.java @@ -19,17 +19,20 @@ public class Tag { public float layoutBorderSize; public int layoutBorderColor; public Drawable background; + public int maxTags; public Tag(String text) { init(0, text, Constants.DEFAULT_TAG_TEXT_COLOR, Constants.DEFAULT_TAG_TEXT_SIZE, Constants.DEFAULT_TAG_LAYOUT_COLOR, Constants.DEFAULT_TAG_LAYOUT_COLOR_PRESS, - Constants.DEFAULT_TAG_IS_DELETABLE, Constants.DEFAULT_TAG_DELETE_INDICATOR_COLOR, Constants.DEFAULT_TAG_DELETE_INDICATOR_SIZE, Constants.DEFAULT_TAG_RADIUS, Constants.DEFAULT_TAG_DELETE_ICON, Constants.DEFAULT_TAG_LAYOUT_BORDER_SIZE, Constants.DEFAULT_TAG_LAYOUT_BORDER_COLOR); + Constants.DEFAULT_TAG_IS_DELETABLE, Constants.DEFAULT_TAG_DELETE_INDICATOR_COLOR, Constants.DEFAULT_TAG_DELETE_INDICATOR_SIZE, Constants.DEFAULT_TAG_RADIUS, + Constants.DEFAULT_TAG_DELETE_ICON, Constants.DEFAULT_TAG_LAYOUT_BORDER_SIZE, Constants.DEFAULT_TAG_LAYOUT_BORDER_COLOR, + Constants.DEFAULT_MAX_TAGS); } private void init(int id, String text, int tagTextColor, float tagTextSize, int layoutColor, int layoutColorPress, boolean isDeletable, int deleteIndicatorColor,float deleteIndicatorSize, float radius, - String deleteIcon, float layoutBorderSize, int layoutBorderColor) { + String deleteIcon, float layoutBorderSize, int layoutBorderColor, int maxTags) { this.id = id; this.text = text; this.tagTextColor = tagTextColor; @@ -43,5 +46,6 @@ private void init(int id, String text, int tagTextColor, float tagTextSize, this.deleteIcon = deleteIcon; this.layoutBorderSize = layoutBorderSize; this.layoutBorderColor = layoutBorderColor; + this.maxTags = maxTags; } } diff --git a/library/src/main/java/com/cunoraz/tagview/TagView.java b/library/src/main/java/com/cunoraz/tagview/TagView.java index 902944d..c4b99e7 100644 --- a/library/src/main/java/com/cunoraz/tagview/TagView.java +++ b/library/src/main/java/com/cunoraz/tagview/TagView.java @@ -8,6 +8,7 @@ import android.graphics.drawable.StateListDrawable; import android.os.Build; import android.util.AttributeSet; +import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; @@ -59,6 +60,7 @@ public class TagView extends RelativeLayout { private int textPaddingRight; private int textPaddingTop; private int textPaddingBottom; + private int maxTags; /** @@ -122,6 +124,7 @@ public void onGlobalLayout() { this.textPaddingRight = (int) typeArray.getDimension(R.styleable.TagView_textPaddingRight, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_TEXT_PADDING_RIGHT)); this.textPaddingTop = (int) typeArray.getDimension(R.styleable.TagView_textPaddingTop, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_TEXT_PADDING_TOP)); this.textPaddingBottom = (int) typeArray.getDimension(R.styleable.TagView_textPaddingBottom, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_TEXT_PADDING_BOTTOM)); + this.maxTags = typeArray.getInteger(R.styleable.TagView_maxTags, Constants.DEFAULT_MAX_TAGS); typeArray.recycle(); } @@ -157,6 +160,7 @@ protected void onDraw(Canvas canvas) { /** * tag draw */ + private void drawTags() { if (!mInitialized) { @@ -172,6 +176,8 @@ private void drawTags() { int listIndex = 1;// List Index int indexBottom = 1;// The Tag to add below int indexHeader = 1;// The header tag of this line + int tagsDrawn = 0;// The number of tags drawn in a single line + Tag tagPre = null; for (Tag item : mTags) { final int position = listIndex - 1; @@ -247,14 +253,30 @@ public void onClick(View v) { //add margin of each line tagParams.bottomMargin = lineMargin; - if (mWidth <= total + tagWidth + Utils.dipToPx(this.getContext(), Constants.LAYOUT_WIDTH_OFFSET)) { + boolean ifWidthExceed; + // checks if maxTags is specified by the user + if (maxTags != Constants.DEFAULT_MAX_TAGS) { + //checks if tags drawn in one line is less than max tags to be drawn + if (tagsDrawn < maxTags) { + ifWidthExceed = false; + } else { + ifWidthExceed = true; + } + } else { + // if total width of the tag doesn't exceed the screen width + ifWidthExceed = mWidth <= total + tagWidth + Utils.dipToPx(this.getContext(), Constants.LAYOUT_WIDTH_OFFSET); + } + + if (ifWidthExceed) { //need to add in new line if (tagPre != null) tagParams.addRule(RelativeLayout.BELOW, indexBottom); // initialize total param (layout padding left & layout padding right) total = getPaddingLeft() + getPaddingRight(); indexBottom = listIndex; indexHeader = listIndex; + tagsDrawn = 1; } else { + tagsDrawn+=1; //no need to new line tagParams.addRule(RelativeLayout.ALIGN_TOP, indexHeader); //not header of the line @@ -266,8 +288,6 @@ public void onClick(View v) { indexBottom = listIndex; } } - - } total += tagWidth; addView(tagLayout, tagParams); @@ -410,6 +430,14 @@ public void settextPaddingBottom(float textPaddingBottom) { this.textPaddingBottom = Utils.dipToPx(getContext(), textPaddingBottom); } + public int getMaxTags() { + return maxTags; + } + + public void setMaxTags(int maxTags) { + this.maxTags = maxTags; + } + /** * setter for OnTagLongClickListener diff --git a/library/src/main/res/values/tagview_attr.xml b/library/src/main/res/values/tagview_attr.xml index 73825de..56efcee 100644 --- a/library/src/main/res/values/tagview_attr.xml +++ b/library/src/main/res/values/tagview_attr.xml @@ -7,5 +7,6 @@ + \ No newline at end of file From bb4e5bef13b42924c6441d12ba4e98c74d2b1570 Mon Sep 17 00:00:00 2001 From: AkshayMore Date: Thu, 25 May 2017 18:46:10 +0530 Subject: [PATCH 2/2] added functionality to specify max tags in a single line Note: since user specifically wants said number of tags in one line, the tag won't be place in new line if it exceeds the screen width. --- app/app.iml | 15 ++------------ app/src/main/res/layout/activity_main.xml | 24 +++++++++++++---------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/app/app.iml b/app/app.iml index e7e9759..811fa8a 100644 --- a/app/app.iml +++ b/app/app.iml @@ -9,7 +9,6 @@