Search code examples
androidtextviewspannablestring

How to detect if TextView contains SpannableStringBuilder


How can one find out if TextView contains SpannableStringBuilder and not plain text? I need to change text size of TextView, but it is different if it contains plain text or span.

TextView textView1;
textView1.setText("BlahBlahBlah");

TextView textView2;
final SpannableStringBuilder sb = new SpannableStringBuilder(title);
                                                        final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255,255,255));
                                                        sb.setSpan(fcs,  bookname.length(),title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView2.setText(sb);

Now I need a method like this : textView1.hasSpans()


Solution

  • use instanceOf

     final CharSequence text = textView.getText();
     if(text instanceof Spannable){
        ...
    

    I'm using it in setFilter so no reason why it will not work over here