user3713442
user3713442

Reputation: 498

Android Textview altering line spacing/padding

I'm trying to create a textview within a bottomsheetdialog, with example layout as shown below. I've put my desired formatting notes in brackets Most of the spanned texts (bold, underlined etc.) are easy to create

Linespacing of 2 is easy with "\n\n", but how do I create 1.5 line spacing between certain lines? I'm adding text dynamically, using append(...), so I can't use the android:text atribute in the layout xml. Most of the examples I've seen have line spacing hard-wired into xml too which applies to all the text. I've found that you can apply different line spacings for paragraphs, but the "\n" would break the paragraph and the different line spacing won't work.

title (underlined, bold, very large)
(linespace 1.5)
blurb
(linespace 2)
sub-section1 (underlined, bold, large)
(linespace 1.5)
ClickableSubheading1 
(linespace 1.5)
clickableSubheading2
(linespace 1.5)
[could be many more subheadings here...]

(linespace 2)
sub-section2 (underlined, bold, large)
(linespace 1.5)
ClickableSubheading1a 
(linespace 1.5)
clickableSubheading2a
(linespace 1.5)
[could be many more subheadings here...]


etc - more subsections to be added as and when needed

NB: the clickable sub headings apply code implicit to the app, and are not hyperlinks (I thought about doing the whole lot as html and then reading that into my textview) Basically, what I need is a span, or similar that says "put a linebreak, m times '\n' here", where 'm' can be any value

Upvotes: 1

Views: 2346

Answers (2)

Linh
Linh

Reputation: 60913

You can use RelativeSizeSpan to scale the size of the second '\n' to smaller value.

Like

// I try to find all '\n\n' in TextView then I resize the second \n size to 0.5
private fun beautifulParagraph(textView: TextView) {
    val text = textView.text
    var startIndex = 0
    val sb = SpannableString(text)
    var index: Int
    while (text.indexOf("\n\n", startIndex).also { index = it } != -1) {
        sb.setSpan(RelativeSizeSpan(0.5f), index + 1, index + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        startIndex = index + 2
    }
    textView.text = sb
}

Using

beautifulParagraph(yourTextView)

XML

<TextView
        android:id="@+id/tv_heelo"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\nbbbbbbbbbbbbbbbnbbbbbbbbbbbbbbbnbbbbbbbbbbbbbbbnbbbbbbbbbbbbbbb\n\nccccccccccccccccnccccccccccccccccnccccccccccccccccnccccccccccccccccncccccccccccccccc\n\nddd"
        android:textSize="15sp"
        android:layout_margin="20dp"
        android:background="#000"
        android:textColor="#fff"
/>

Result (without resize and with resize)

------>

Upvotes: 7

Mahmoud Waked
Mahmoud Waked

Reputation: 395

  1. You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.
  2. You can use Span to customize what you want in textview https://developer.android.com/guide/topics/text/spans

  3. Or you can custom what you want in HTML and display HTML in TextView

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        textView.setText(Html.fromHtml(htmlString, Html.FROM_HTML_MODE_COMPACT));
    } else { 
        textView.setText(Html.fromHtml(htmlString));
    }
    

These what can i suggestion for you.

Upvotes: 0

Related Questions