How to add horizontal spacing between two Spannables?

44 views Asked by At

I have a custom TextView that combines two pieces of text and have a spacing variable.

class MyCustomTextView() {

  var label: String
  var value: String
  var spacingBetweenLabelAndValue: Int = 0

  // ...
}

Each one have their own style and are concatenated. Example:

  • label -> "Name:"
  • value -> "John"
this.text = Spanny(label, *labelTextSpannables)
      .append(value, *valueTextSpannables)

which in the end is resulting in "Name:John"

I want to add an extra spacing (spacingBetweenLabelAndValue) between those two strings.

"Name:(spacing here)John"

Is there a way to do this using Spannable?

1

There are 1 answers

1
Ali On
 SpannableStringBuilder("label")
.append(String.format("%1$" + spacingBetweenLabelAndValue + "s", ""))
.append(SpannableString("value"))