diff --git a/README.md b/README.md index 7558ad6..9b382e4 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,20 @@ List allHashTags = mTextHashTagHelper.getAllHashTags(true); // get all hashtags without "#", example: "blueeyes" List allHashTags = mTextHashTagHelper.getAllHashTags(false); ``` + +To add custom starting tags (eg : '@','$' etc...) +``` +List startingChars = new ArrayList<>(); +startingChars.add('@'); +startingChars.add('#'); +startingChars.add('-'); +mHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimaryDark), null, startingChars); + +//Or you can simply use arrays helper like this one +mHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimaryDark), null, Arrays.asList('#','@','-')); +``` #Demo -![alt tag](https://cloud.githubusercontent.com/assets/2686355/11998408/e6aa1f62-aaa6-11e5-911a-c598b6853862.gif) ![alt tag](https://cloud.githubusercontent.com/assets/2686355/11998409/e6aa3ed4-aaa6-11e5-9797-100a024659cc.gif) +![alt tag](https://cloud.githubusercontent.com/assets/2686355/11998408/e6aa1f62-aaa6-11e5-911a-c598b6853862.gif) ![alt tag](https://cloud.githubusercontent.com/assets/2686355/11998409/e6aa3ed4-aaa6-11e5-9797-100a024659cc.gif) ![new version](https://cloud.githubusercontent.com/assets/8886687/18061116/bbd3e1e8-6e36-11e6-9d4d-376b1399bd7b.jpg) # License diff --git a/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/ClickableForegroundColorSpan.java b/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/ClickableForegroundColorSpan.java index cbba84a..190652d 100644 --- a/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/ClickableForegroundColorSpan.java +++ b/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/ClickableForegroundColorSpan.java @@ -18,10 +18,6 @@ public class ClickableForegroundColorSpan extends ClickableSpan { private OnHashTagClickListener mOnHashTagClickListener; - public interface OnHashTagClickListener { - void onHashTagClicked(String hashTag); - } - private final int mColor; public ClickableForegroundColorSpan(@ColorInt int color, OnHashTagClickListener listener) { diff --git a/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/HashTagHelper.java b/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/HashTagHelper.java index a3062dc..1e33598 100644 --- a/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/HashTagHelper.java +++ b/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/HashTagHelper.java @@ -10,6 +10,7 @@ import android.widget.TextView; import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -24,7 +25,7 @@ * #hashtagendsifitfindsnotletterornotdigitsignlike_thisIsNotHighlithedArea * */ -public final class HashTagHelper implements ClickableForegroundColorSpan.OnHashTagClickListener { +public final class HashTagHelper implements OnHashTagClickListener { /** * If this is not null then all of the symbols in the List will be considered as valid symbols of hashtag @@ -38,6 +39,7 @@ public final class HashTagHelper implements ClickableForegroundColorSpan.OnHashT private final List mAdditionalHashTagChars; private TextView mTextView; private int mHashTagWordColor; + private List mStartChars; private OnHashTagClickListener mOnHashTagClickListener; @@ -46,17 +48,18 @@ public static final class Creator{ private Creator(){} public static HashTagHelper create(int color, OnHashTagClickListener listener){ - return new HashTagHelper(color, listener, null); + return new HashTagHelper(color, listener, Arrays.asList('#'), null); + } + public static HashTagHelper create(int color, OnHashTagClickListener listener,List startChars){ + return new HashTagHelper(color, listener, startChars, null); } public static HashTagHelper create(int color, OnHashTagClickListener listener, char... additionalHashTagChars){ - return new HashTagHelper(color, listener, additionalHashTagChars); + return new HashTagHelper(color, listener, Arrays.asList('#'), additionalHashTagChars); + } + public static HashTagHelper create(int color, OnHashTagClickListener listener,List startChar, char... additionalHashTagChars){ + return new HashTagHelper(color, listener, startChar, additionalHashTagChars); } - - } - - public interface OnHashTagClickListener{ - void onHashTagClicked(String hashTag); } private final TextWatcher mTextWatcher = new TextWatcher() { @@ -76,10 +79,11 @@ public void afterTextChanged(Editable s) { } }; - private HashTagHelper(int color, OnHashTagClickListener listener, char... additionalHashTagCharacters) { + private HashTagHelper(int color, OnHashTagClickListener listener,List startChars, char... additionalHashTagCharacters) { mHashTagWordColor = color; mOnHashTagClickListener = listener; mAdditionalHashTagChars = new ArrayList<>(); + mStartChars = startChars; if(additionalHashTagCharacters != null){ for(char additionalChar : additionalHashTagCharacters){ @@ -133,7 +137,8 @@ private void setColorsToAllHashTags(CharSequence text) { while (index < text.length()- 1){ char sign = text.charAt(index); int nextNotLetterDigitCharIndex = index + 1; // we assume it is next. if if was not changed by findNextValidHashTagChar then index will be incremented by 1 - if(sign == '#'){ + + if(containsOne(mStartChars,sign)){ startIndexOfNextHashSign = index; nextNotLetterDigitCharIndex = findNextValidHashTagChar(text, startIndexOfNextHashSign); @@ -203,6 +208,13 @@ public List getAllHashTags() { return getAllHashTags(false); } + private boolean containsOne(List charsList,char targetChar){ + for(char c : charsList){ + if(targetChar == c) + return true; + } + return false; + } @Override public void onHashTagClicked(String hashTag) { mOnHashTagClickListener.onHashTagClicked(hashTag); diff --git a/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/OnHashTagClickListener.java b/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/OnHashTagClickListener.java new file mode 100644 index 0000000..2940f11 --- /dev/null +++ b/hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/OnHashTagClickListener.java @@ -0,0 +1,5 @@ +package com.volokh.danylo.hashtaghelper; + +public interface OnHashTagClickListener { + void onHashTagClicked(String hashTag); +} diff --git a/sample_app/src/main/java/com/volokh/danylo/example/HashTagHelperDemoActivity.java b/sample_app/src/main/java/com/volokh/danylo/example/HashTagHelperDemoActivity.java index f90782f..ca607ca 100644 --- a/sample_app/src/main/java/com/volokh/danylo/example/HashTagHelperDemoActivity.java +++ b/sample_app/src/main/java/com/volokh/danylo/example/HashTagHelperDemoActivity.java @@ -1,18 +1,22 @@ package com.volokh.danylo.example; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; +import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.volokh.danylo.hashtaghelper.HashTagHelper; +import com.volokh.danylo.hashtaghelper.OnHashTagClickListener; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -public class HashTagHelperDemoActivity extends AppCompatActivity implements HashTagHelper.OnHashTagClickListener, View.OnClickListener { +public class HashTagHelperDemoActivity extends AppCompatActivity implements OnHashTagClickListener, View.OnClickListener { private static final String TAG = HashTagHelperDemoActivity.class.getSimpleName(); @@ -32,7 +36,7 @@ protected void onCreate(Bundle savedInstanceState) { mHashTagText = (TextView) findViewById(R.id.text); mAllHashTag = (TextView) findViewById(R.id.all_hashtags); - mEditTextView = (TextView) findViewById(R.id.edit_text_field); + mEditTextView = (EditText) findViewById(R.id.edit_text_field); Button getEnteredText = (Button) findViewById(R.id.get_entered_text_btn); getEnteredText.setOnClickListener(this); @@ -45,11 +49,15 @@ protected void onCreate(Bundle savedInstanceState) { }; // If you set additional symbols not only letters and digits will be a valid symbols for hashtag // Example: "hash_tag_with_underscore_and$dolar$sign$is$also$valid_hashtag" - mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimary), this, additionalSymbols); + mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimaryDark), this, Arrays.asList('#','@','-'), additionalSymbols); mTextHashTagHelper.handle(mHashTagText); // Here we don't specify additionalSymbols. It means that in EditText only letters and digits will be valid symbols - mEditTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimaryDark), null); + List startingChars = new ArrayList<>(); + startingChars.add('@'); + startingChars.add('#'); + startingChars.add('-'); + mEditTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimaryDark), null, Arrays.asList('#','@','-')); mEditTextHashTagHelper.handle(mEditTextView); }