Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix adding tags if the app language is RTL #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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.1'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
52 changes: 33 additions & 19 deletions library/src/main/java/com/cunoraz/tagview/TagView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public class TagView extends RelativeLayout {
*/
private int lineMargin;
private int tagMargin;
private int textPaddingLeft;
private int textPaddingRight;
private int textPaddingStart;
private int textPaddingEnd;
private int textPaddingTop;
private int textPaddingBottom;

Expand Down Expand Up @@ -118,8 +118,8 @@ public void onGlobalLayout() {
TypedArray typeArray = ctx.obtainStyledAttributes(attrs, R.styleable.TagView, defStyle, defStyle);
this.lineMargin = (int) typeArray.getDimension(R.styleable.TagView_lineMargin, Utils.dipToPx(this.getContext(), Constants.DEFAULT_LINE_MARGIN));
this.tagMargin = (int) typeArray.getDimension(R.styleable.TagView_tagMargin, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_MARGIN));
this.textPaddingLeft = (int) typeArray.getDimension(R.styleable.TagView_textPaddingLeft, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_TEXT_PADDING_LEFT));
this.textPaddingRight = (int) typeArray.getDimension(R.styleable.TagView_textPaddingRight, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_TEXT_PADDING_RIGHT));
this.textPaddingStart = (int) typeArray.getDimension(R.styleable.TagView_textPaddingStart, Utils.dipToPx(this.getContext(), Constants.DEFAULT_TAG_TEXT_PADDING_LEFT));
this.textPaddingEnd = (int) typeArray.getDimension(R.styleable.TagView_textPaddingEnd, 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));
typeArray.recycle();
Expand Down Expand Up @@ -167,7 +167,12 @@ private void drawTags() {
removeAllViews();

// layout padding left & layout padding right
float total = getPaddingLeft() + getPaddingRight();
float total;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
total = getPaddingStart() + getPaddingEnd();
} else {
total = getPaddingLeft() + getPaddingRight();
}

int listIndex = 1;// List Index
int indexBottom = 1;// The Tag to add below
Expand All @@ -191,7 +196,7 @@ private void drawTags() {
TextView tagView = (TextView) tagLayout.findViewById(R.id.tv_tag_item_contain);
tagView.setText(tag.text);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tagView.getLayoutParams();
params.setMargins(textPaddingLeft, textPaddingTop, textPaddingRight, textPaddingBottom);
params.setMargins(textPaddingStart, textPaddingTop, textPaddingEnd, textPaddingBottom);
tagView.setLayoutParams(params);
tagView.setTextColor(tag.tagTextColor);
tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, tag.tagTextSize);
Expand All @@ -216,7 +221,7 @@ public boolean onLongClick(View v) {
});

// calculate of tag layout width
float tagWidth = tagView.getPaint().measureText(tag.text) + textPaddingLeft + textPaddingRight;
float tagWidth = tagView.getPaint().measureText(tag.text) + textPaddingStart + textPaddingEnd;
// tagView padding (left & right)

// deletable text
Expand All @@ -225,7 +230,7 @@ public boolean onLongClick(View v) {
deletableView.setVisibility(View.VISIBLE);
deletableView.setText(tag.deleteIcon);
int offset = Utils.dipToPx(getContext(), 2f);
deletableView.setPadding(offset, textPaddingTop, textPaddingRight + offset, textPaddingBottom);
deletableView.setPadding(offset, textPaddingTop, textPaddingEnd + offset, textPaddingBottom);
deletableView.setTextColor(tag.deleteIndicatorColor);
deletableView.setTextSize(TypedValue.COMPLEX_UNIT_SP, tag.deleteIndicatorSize);
deletableView.setOnClickListener(new OnClickListener() {
Expand All @@ -236,7 +241,7 @@ public void onClick(View v) {
}
}
});
tagWidth += deletableView.getPaint().measureText(tag.deleteIcon) + textPaddingLeft + textPaddingRight;
tagWidth += deletableView.getPaint().measureText(tag.deleteIcon) + textPaddingStart + textPaddingEnd;
// deletableView Padding (left & right)
} else {
deletableView.setVisibility(View.GONE);
Expand All @@ -251,16 +256,25 @@ public void onClick(View v) {
//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();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
total = getPaddingStart() + getPaddingEnd();
} else {
total = getPaddingLeft() + getPaddingRight();
}
indexBottom = listIndex;
indexHeader = listIndex;
} else {
//no need to new line
tagParams.addRule(RelativeLayout.ALIGN_TOP, indexHeader);
//not header of the line
if (listIndex != indexHeader) {
tagParams.addRule(RelativeLayout.RIGHT_OF, listIndex - 1);
tagParams.leftMargin = tagMargin;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
tagParams.addRule(RelativeLayout.END_OF, listIndex - 1);
tagParams.setMarginStart(tagMargin);
} else {
tagParams.addRule(RelativeLayout.RIGHT_OF, listIndex - 1);
tagParams.leftMargin = tagMargin;
}
total += tagMargin;
if (tagPre.tagTextSize < tag.tagTextSize) {
indexBottom = listIndex;
Expand Down Expand Up @@ -376,20 +390,20 @@ public void setTagMargin(float tagMargin) {
this.tagMargin = Utils.dipToPx(getContext(), tagMargin);
}

public int getTextPaddingLeft() {
return textPaddingLeft;
public int getTextPaddingStart() {
return textPaddingStart;
}

public void setTextPaddingLeft(float textPaddingLeft) {
this.textPaddingLeft = Utils.dipToPx(getContext(), textPaddingLeft);
this.textPaddingStart = Utils.dipToPx(getContext(), textPaddingLeft);
}

public int getTextPaddingRight() {
return textPaddingRight;
public int getTextPaddingEnd() {
return textPaddingEnd;
}

public void setTextPaddingRight(float textPaddingRight) {
this.textPaddingRight = Utils.dipToPx(getContext(), textPaddingRight);
public void setTextPaddingEnd(float textPaddingEnd) {
this.textPaddingEnd = Utils.dipToPx(getContext(), textPaddingEnd);
}

public int getTextPaddingTop() {
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/res/values/tagview_attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<declare-styleable name="TagView">
<attr name="lineMargin" format="dimension" />
<attr name="tagMargin" format="dimension" />
<attr name="textPaddingLeft" format="dimension" />
<attr name="textPaddingRight" format="dimension" />
<attr name="textPaddingStart" format="dimension" />
<attr name="textPaddingEnd" format="dimension" />
<attr name="textPaddingTop" format="dimension" />
<attr name="textPaddingBottom" format="dimension" />
</declare-styleable>
Expand Down