2

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?

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
Hojjat
  • 815
  • 1
  • 10
  • 25

1 Answers1

4

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.

Community
  • 1
  • 1
KunalK
  • 1,904
  • 4
  • 22
  • 40
  • 1
    I solve this problem in Android M by adding font spacing to textview instead of applying LineHeightSpan. You saved my time. Thank you. – Hojjat Jul 24 '16 at 12:59