Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setSpan() in onTextChanged() to save parameters of onTextChanged()?

I am actually trying to do card formatting, for that I am trying to implement what google says from link

You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.

From above what I understand is I need to save [CharSequence s, int start, int before, int count] using setSpan in onTextChanged() and somehow retrieve them back in afterTextChanged().

Question is, on which object do I call setSpan() in onTextChanged() and how do I retrieve those saved values in afterTextChanged().

like image 626
T_C Avatar asked Oct 15 '14 23:10

T_C


1 Answers

So I figured this one out. Below is a barebones example that can be used as a starting point.

import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.RelativeSizeSpan;

public class AwesomeTextWatcherDeluxe implements TextWatcher {

    private RelativeSizeSpan span;
    private SpannableString spannable;


    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // We can use any span here. I use RelativeSizeSpan with size 1, which makes absolutely nothing.
        // Perhaps there are other spans better suited for it?
        span = new RelativeSizeSpan(1.0f);
        spannable = new SpannableString(s);
        spannable.setSpan(span, start, start + count, Spanned.SPAN_COMPOSING);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        int beginIndex = spannable.getSpanStart(span);
        int endIndex = spannable.getSpanEnd(span);

        // Do your changes here.

        // Cleanup
        spannable.removeSpan(span);
        span = null;
        spannable = null;
    }
}
like image 52
MW. Avatar answered Nov 17 '22 22:11

MW.