Viktor
Viktor

Reputation: 646

How to apply spans to the whole Spannable string

Everywhere I see only these ways:

text[0..3] = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)

or using setSpan, but there I must only use some limits(start and end indexes).

Then I tried this:

text[text.indices] = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)

It works, but it excludes the last character.

How to apply spans to the whole Spannable string?

Upvotes: 0

Views: 333

Answers (2)

user12039920
user12039920

Reputation:

i guss you are looking for this because they must be stored in loop to apply multiple properties in many elements

val list = ArrayOf(text1,text2,text3)// <- store your all text here
list.forEach{
    it = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)
}

or

val list = ArrayOf(text1,text2,text3)// <- store your all text here
for(element in list){
    element = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)
}

or

(0..(list.length - 1)).forEach{
    list[it] = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)
}

or

text.forEach{
    it = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)//this one applied to every char
}

Upvotes: 0

jana
jana

Reputation: 266

You have to use setSpan and define the first and last index.

val span = AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE)
text.setSpan(span, 0, text.length, 0)

and since AlignmentSpan is a ParagraphStyle it must be applied to the whole block of text (which can be split by the newline character \n).

Upvotes: 1

Related Questions