Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether an EditText in Android has emoticon or not?

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 "";
}
like image 645
Yash Gupta Avatar asked Apr 02 '14 09:04

Yash Gupta


People also ask

How can I tell if EditText is focusable android?

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.

What is the difference between EditText and TextView in android?

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.


1 Answers

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) {
    }

};
like image 75
Dmitry Ryadnenko Avatar answered Nov 10 '22 08:11

Dmitry Ryadnenko