Hanbit the Developer

[Kotlin] How to make 'instagram comment layout' using Span. 본문

Mobile/Android

[Kotlin] How to make 'instagram comment layout' using Span.

hanbikan 2021. 8. 10. 16:01

https://rccode.tistory.com/entry/Kotlin-Set-indent-to-first-line-of-TextView-in-android

 

[Kotlin] Set indent to first line of TextView in android

 - Description To do so, you should use Span, LeadingMarginSpan. In a nutshell, Span is used to set text style in detail, and LeadingMarginSpan is one of the styles that is related to margin, espe..

rccode.tistory.com

This post is advanced version of above link. So, If you want to get some more descriptions for it in detail, visit the link.

 

 - Description

Before starting, you should know that there are 2 text views(text_nickname, text_content), and they are overlapped like this.

The text_content has a indent only for a first line.

val spannable = SpannableString(contentTextView.text)
val span = LeadingMarginSpan.Standard(nicknameTextView.width + 10, 0)
spannable.setSpan(span, 0, spannable.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

contentTextView.text = spannable

Use SpannableString and LeadingMarginSpan referencing nicknameTextView.width.

 

 - Result