Hojjat Hojjat - 4 months ago 32
Android Question

LineHeightSpan in Android M and lower versions

I wanna add multiple span to a textview with configurable line space. I use LineHeightSpan class defined in android. But it seems it behave differently in Android M and lower versions
Here the result in lower version of Android

enter image description here

and this is how it looks like in Android M
enter image description here

Here is how I implemented in code:
This is my implementation of LineHeightSpan:

public class VerticalMarginSpan implements LineHeightSpan {

private final int value;

public VerticalMarginSpan(int value) {
this.value = value;
}

@Override
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
Paint.FontMetricsInt fm) {
fm.ascent -= value / 2;
fm.descent += (value / 2);
}
}


And how I use it:

SpannableStringBuilder builder = new SpannableStringBuilder(tempStr);
builder.setSpan(
new VerticalMarginSpan(lineSpace),
0,
tempStr.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(builder);


How I can fix the problem?

Answer

If you are having Marshmallow v6.0 i afraid you can not do anything in this case as it's bug reported Here. You either have to upgrade your device to v6.0.1 as it has been fixed in that version Or conditionally remove your logic from Marshmallow 6.0

EDIT : for more reference check this.