Search code examples
androidmarginsstaticlayoutleadingmarginspan2

What is Leading Margin in Android?


What is the meaning of leading margin as in

The documentation for LeadingMarginSpan says

A paragraph style affecting the leading margin. There can be multiple leading margin spans on a single paragraph; they will be rendered in order, each adding its margin to the ones before it. The leading margin is on the right for lines in a right-to-left paragraph.

But it doesn't really say what leading margin is.

Is it like a tab indent on the first line of the paragraph? Or is it where the whole paragraph is indented? I assume it is /lidɪŋ/ and not /lɛdɪŋ/ as in the space between lines.

The reason I am wondering is that I am trying to make my own TextView with a StaticLayout. I am referring to the Layout and StaticLayout source code for ideas. I'm trying to cut out all unnecessary parts but I didn't know what this was.

Here are a few SO questions that also ask about leading margin, but the askers seem to know what it means.

An image would be really helpful, but not strictly necessary.


Solution

  • Leading margin refers to how much a paragraph is indented, both the first line and the subsequent lines.

    The following examples should make everything clear. The TextViews in the examples below contain two paragraphs of text (ie, they include the \n character).

    Here is the boilerplate code that was used:

    LeadingMarginSpan span = ... // substitute this line with the examples below
    
    TextView textView = (TextView) findViewById(R.id.textView) ;
    SpannableString spannableString = new SpannableString("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
    spannableString.setSpan(span, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString);
    

    LeadingMarginSpan.Standard

    There are two main constructors.

    First constructor: LeadingMarginSpan.Standard(int first, int rest)

    • first tells how many pixels to indent the first line of each paragraph.
    • rest tells how many pixels to indent the rest of the lines of each paragraph.

    enter image description here

    The example on the left indents the first line by 20 pixels and the rest of the lines by 100 pixels. (No padding has been added to the TextView.)

    LeadingMarginSpan span = new LeadingMarginSpan.Standard(20, 100); // left example
    

    The example on the right shows the first line indented by 100 and the rest of the lines not indented at all.

    LeadingMarginSpan span = new LeadingMarginSpan.Standard(100, 0); // right exmaple
    

    Second constructor: LeadingMarginSpan.Standard(int every)

    • every tells how many pixels to indent every line of each paragraph.

    enter image description here

    This example indents every line by 200 pixels.

    LeadingMarginSpan span = new LeadingMarginSpan.Standard(200);