I need to check whether an edit text has some emoticon in it or not. I tried making a text watcher in which I checked whether an Image span is present but I am unable to get any results.
SpannableStringBuilder s = new SpannableStringBuilder(source.toString());
ImageSpan a[]= s.getSpans(0,s.length(), ImageSpan.class);
if(a.length!=0){
Toast.makeText(NewEpisodeActivity.this, R.string.invalid_char, Toast.LENGTH_SHORT).show();
return "";
}
You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts.
EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.
Do check in afterTextChanged(Editable editable)
not in onTextChanged()
private TextWatcher textChangedListener = new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
final ImageSpan[] itemSpans = editable.getSpans(0, editable.length(), ImageSpan.class);
final boolean hasEmoticons = itemSpans != null && itemSpans.length > 0;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With