Reputation: 1876
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;
}
Upvotes: 0
Views: 836
Reputation: 1876
As a reference to above answer although it's a great answer but it'll always toast the last hashtag means let's take an example if the entered text is abc #hi #hello
even if we'll move the cursor to #hi
and change it to #bye
it would toast the las tag i.e #hello
so for solving this we can use current cursor position
So the final code is :
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) {
}
@Override
public void afterTextChanged(Editable s)
{
String sampleText = s.toString().substring(0,editTxt.getSelectionStart());
String[] wordSplit = sampleText.split(" ");
String sign=null;
for (int i = wordSplit.length-1; i >= 0; i--){
if(wordSplit[i].contains("#")){
Toast.makeText(getApplicationContext(), wordSplit[i].substring(wordSplit[i].lastIndexOf("#")), Toast.LENGTH_SHORT).show();
break;
}
}
}
});
Upvotes: 0
Reputation: 753
Try this one
String sampleText = "abc #hello #hi #bye";
String[] wordSplit = sampleText.split(" ");
for (int i = wordSplit.length-1; i >= 0; i--){
if(wordSplit[i].contains("#")){
Toast.makeText(getContext(), wordSplit[i].substring(wordSplit[i].indexOf("#")), Toast.LENGTH_SHORT).show();
break;
}
}
Edit: try using this instead of indexOf
lastIndexOf("#")
Upvotes: 4
Reputation: 309
try this : may help you
String sampleText = "abc#hello#hi#bye";
String[] wordSplit = sampleText.split("#");
for (int i = wordSplit.length - 1; i >= 0; i--) {
Toast.makeText(this, wordSplit[wordSplit.length - 1], Toast.LENGTH_SHORT).show();
break;
}
Upvotes: -1
Reputation: 1474
So your TextWatcher
will look like this
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) {
}
@Override
public void afterTextChanged(Editable s)
{
String[] wordSplit = s.toString().split(" ");
for (int i = wordSplit.length-1; i >= 0; i--){
if(wordSplit[i].contains("#")){
Toast.makeText(getApplicationContext(), wordSplit[i], Toast.LENGTH_SHORT).show();
break;
}
}
}
});
Upvotes: -1