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().
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;
}
}
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