7

I use a basic and very common implementation of a LeadingMarginSpan2 to wrap text around an image (since it seems to be the easiest way and please don't suggest me to use WebViews at this point):

  public class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {

  private int margin;
  private int lines;

  public MyLeadingMarginSpan2(int lines, int margin) {
    this.margin = margin;
    this.lines = lines;
  }


  @Override
  public int getLeadingMargin(boolean first) {
    return first ? margin : 0; // <--- the issue is here
  }


  @Override
  public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
                              int top, int baseline, int bottom, CharSequence text,
                              int start, int end, boolean first, Layout layout) {
  }


  @Override
  public int getLeadingMarginLineCount() {
    return lines;
  }
 }

The problem is that as soon as a paragraph occurs in the text, an unwanted margin is given to this line. I want to limit the number of times getLeadingMargin() returns the actual margin to the number of lines passed inside the constructor.

I've tried to count how many times this margin was returned and compare it against the number of lines, this didn't work however (in most cases no margin was applied, in some cases it was applied to a wrong number of lines).

Does anyone have a workaround for this issue?

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • *"The problem is that as soon as a paragraph occurs in the text (...)"* - Isn't that exactly what `LeadingMarginSpan2` is *not* designed for? As per the documentation, there should only be one `LeadingMarginSpan2` per paragraph. The leading margin line count affects all LeadingMarginSpans in the paragraph, adjusting the number of lines to which the first line margin is applied. – MH. Dec 06 '14 at 18:34
  • @MH I noticed it's not really designed for that, but I don't see any other possibility to wrap a text around an image w/o using `WebView` or relying on 3rd party half-working solutions. That's why I asked for a workaround with a `LeadingMarginSpan2` – Droidman Dec 06 '14 at 19:58
  • @Droidman Did you find a solution to this? – Yoann Hercouet Jun 28 '16 at 12:55
  • @YoannHercouet as far as I remember no, I just ended up redesigning the layout to avoid the need of text wrapping. – Droidman Jun 28 '16 at 15:36
  • @MH. could you help me with setting 'wrap text around image' for image placed on the right side of the screen, please? – Ramona Aug 10 '17 at 12:00

0 Answers0