I'm a complete beginner in android so please do excuse me if my question is foolish.Basically what I want to do is I want to detect text from occurance of # means for example if user is entering abc #hello
then only #hello
will be toasted in text change . So I tried to take reference from a github code and able to print all the # tags but I want to toast only current tag means if user is typing abc #hello #hi #bye //here current tag is #bye
so I want to toast only the current tag upto the occurance of space on fly . I wonder how to modify my code to get the desired result.
Code:
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>0)
showTags(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
//Methods
private void showTags(CharSequence text) {
int startIndexOfNextHashSign;
int index = 0;
while (index < text.length()- 1){
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=='#'){
startIndexOfNextHashSign = index;
nextNotLetterDigitCharIndex = findNextValidHashTagChar(text, startIndexOfNextHashSign);
Toast.makeText(this,text.subSequence(startIndexOfNextHashSign,nextNotLetterDigitCharIndex),Toast.LENGTH_LONG).show();
//setColorForHashTagToTheEnd(startIndexOfNextHashSign, nextNotLetterDigitCharIndex);
}
index = nextNotLetterDigitCharIndex;
}
}
private int findNextValidHashTagChar(CharSequence text, int start) {
int nonLetterDigitCharIndex = -1; // skip first sign '#"
for (int index = start + 1; index < text.length(); index++) {
char sign = text.charAt(index);
boolean isValidSign = Character.isLetterOrDigit(sign) || mAdditionalHashTagChars.contains(sign);
if (!isValidSign) {
nonLetterDigitCharIndex = index;
break;
}
}
if (nonLetterDigitCharIndex == -1) {
// we didn't find non-letter. We are at the end of text
nonLetterDigitCharIndex = text.length();
}
return nonLetterDigitCharIndex;
}