How to use specified paragraph spacing with StaticLayout in Android

422 views Asked by At

I have a long text which has multiple paragraph in it. But I want to use specified (eventually more than line spacing value) paragraph spacing between two paragraph in the text. Here I am using StaticLayout and drawing on canvas. As there is no parameter to do so, I tried to do it by little hackish ways like below.

Way 1: Extending the StaticLayout and override the some methods. Which was not possible as private and package specific methods are not override-able. like below

/* package */ void generate(Builder b, boolean includepad, boolean trackpad){ ... }

private int out(CharSequence text, int start, int end,
                  int above, int below, int top, int bottom, int v,
                  float spacingmult, float spacingadd,
                  LineHeightSpan[] chooseHt, int[] chooseHtv,
                  Paint.FontMetricsInt fm, int flags,
                  boolean needMultiply, byte[] chdirs, int dir,
                  boolean easy, int bufEnd, boolean includePad,
                  boolean trackPad, char[] chs,
                  float[] widths, int widthStart, TextUtils.TruncateAt ellipsize,
                  float ellipsisWidth, float textWidth,
                  TextPaint paint, boolean moreChars) { 
...
 if (needMultiply && !lastLine) {
         double ex = (below - above) * (spacingmult - 1) + spacingadd;
         if (ex >= 0) {
             extra = (int)(ex + EXTRA_ROUNDING);
         } else {
             extra = -(int)(-ex + EXTRA_ROUNDING);
         }
     } else {
         extra = 0;
     }

     lines[off + START] = start;
     lines[off + TOP] = v;
     lines[off + DESCENT] = below + extra;
 ... 
}

As a result it was not possible to introduce paragraph spacing attribute in code logic by over riding the above methods.

Way 2: By coping the StaticLayout code to my CustomStaticLayout and directly extending Layout class and introduce the paragraph spacing attribute in this case I don't have access to lot of code which is accessible only inside the android.text package or some internal package.

PS: This approach is bad as code is replicated and Open/Close principal[Open to extension and close to modification/replication] is not maintained but this one was desperate try to somehow accomplish the goal.

public class CustomStaticLayout extends Layout { ... }

Here I can't use constant and class like Alignment value "ALIGN_LEFT" and "ALIGN_RIGHT" so can't use the code of StaticLayout directly. And few like below.

 public enum Alignment {
    ALIGN_NORMAL,
    ALIGN_OPPOSITE,
    ALIGN_CENTER,
    /** @hide */
    ALIGN_LEFT,
    /** @hide */
    ALIGN_RIGHT,
}

As well few native methods are used in StaticLayout like below which are also not possible to access from my CustomStaticLayout.

private static native long nNewBuilder();
private static native void nFreeBuilder(long nativePtr);
private static native void nFinishBuilder(long nativePtr);

PS: Please tell if it's possible to use these native method from class CustomStaticLayout

Way 3: How to use HTML.fromhtml to achieve the same goal like specified paragraph spacing if at all possible.

PS: I have very limited knowledge of html, so I really don't know how to use html to specify paragraph spacing. This might be little bit shallow question please pardon me if it irritates you but kindly answer if if possible.

Html.fromHtml("<h2>Title</h2><br><p>Description here</p>")
0

There are 0 answers