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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,20 @@ List<String> allHashTags = mTextHashTagHelper.getAllHashTags(true);
// get all hashtags without "#", example: "blueeyes"
List<String> allHashTags = mTextHashTagHelper.getAllHashTags(false);
```

To add custom starting tags (eg : '@','$' etc...)
```
List<Character> 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -38,6 +39,7 @@ public final class HashTagHelper implements ClickableForegroundColorSpan.OnHashT
private final List<Character> mAdditionalHashTagChars;
private TextView mTextView;
private int mHashTagWordColor;
private List<Character> mStartChars;

private OnHashTagClickListener mOnHashTagClickListener;

Expand All @@ -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<Character> 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<Character> startChar, char... additionalHashTagChars){
return new HashTagHelper(color, listener, startChar, additionalHashTagChars);
}

}

public interface OnHashTagClickListener{
void onHashTagClicked(String hashTag);
}

private final TextWatcher mTextWatcher = new TextWatcher() {
Expand All @@ -76,10 +79,11 @@ public void afterTextChanged(Editable s) {
}
};

private HashTagHelper(int color, OnHashTagClickListener listener, char... additionalHashTagCharacters) {
private HashTagHelper(int color, OnHashTagClickListener listener,List<Character> startChars, char... additionalHashTagCharacters) {
mHashTagWordColor = color;
mOnHashTagClickListener = listener;
mAdditionalHashTagChars = new ArrayList<>();
mStartChars = startChars;

if(additionalHashTagCharacters != null){
for(char additionalChar : additionalHashTagCharacters){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -203,6 +208,13 @@ public List<String> getAllHashTags() {
return getAllHashTags(false);
}

private boolean containsOne(List<Character> charsList,char targetChar){
for(char c : charsList){
if(targetChar == c)
return true;
}
return false;
}
@Override
public void onHashTagClicked(String hashTag) {
mOnHashTagClickListener.onHashTagClicked(hashTag);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.volokh.danylo.hashtaghelper;

public interface OnHashTagClickListener {
void onHashTagClicked(String hashTag);
}
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
Expand All @@ -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<Character> startingChars = new ArrayList<>();
startingChars.add('@');
startingChars.add('#');
startingChars.add('-');
mEditTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimaryDark), null, Arrays.asList('#','@','-'));
mEditTextHashTagHelper.handle(mEditTextView);
}

Expand Down