Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Development: How To Replace Part of an EditText with a Spannable

I'm trying to replace part of an Editable returned from getText() with a span.

I've tried getText().replace() but that's only for CharSequences.

The reason I'm trying to do this is so I can highlight sections of an EditText one after another (after a small delay), instead of going through and highlighting the whole EditText (which can be slow with big files).

Does anyone have a clue about how I'd go about doing this?

like image 938
AlexPriceAP Avatar asked Sep 07 '11 18:09

AlexPriceAP


Video Answer


1 Answers

This minimal size example makes the word 'first' large:

public class SpanTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String dispStr = "I'm the first line\nI'm the second line";
        TextView tv = (TextView) findViewById(R.id.textView1);
        int startSpan = dispStr.indexOf("first");
        int endSpan = dispStr.indexOf("line");
        Spannable spanRange = new SpannableString(dispStr);
        TextAppearanceSpan tas = new TextAppearanceSpan(this, android.R.style.TextAppearance_Large);
        spanRange.setSpan(tas, startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spanRange);
    }
}

You can adapt it to your needs

like image 146
NickT Avatar answered Nov 15 '22 20:11

NickT