I spent hours searching for an answer and have no idea how to solve it. So let's get down to business:
There is an image and a TextView , and I need to pass a TextView around the ImageView as follows:

The first possible solution would be to use https://github.com/deano2390/FlowTextView , but it does not extend TextView , so this library is not suitable for me for a number of reasons.
The second solution would be to use the LeadingMarginSpan.LeadingMarginSpan2 span, but it affects every paragraph for every n lines inside the text (for example, in this answer -> How to expand text to wrap the image ), so I get something like this:

But I wanted to set margin only for the first n lines! Then I decided to implement LeadingMarginSpan.Standart and create a counter and increase it when calling the getLeadingMargin(first: Boolean): Int function. When the counter reaches the desired value, the function returns 0 as the field width. And again a failure! Instead of filling the TextView lines, the text simply moved to the left and did not spread to the end of the view!
UPD: Yes, I used onGlobalLayoutListener here

Well, googling for another solution, I found this answer https://stackoverflow.com/a/166268/2126121/ Well, I did everything as described and implemented the code:
//set left margin of desirable width val params: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) params.leftMargin = holder.imageContainerHeight!! params.addRule(RelativeLayout.BELOW, holder.mNumberAndTimeInfo!!.id) holder.mCommentTextView!!.layoutParams = params if (holder.commentTextViewOnGlobalLayoutListener != null) holder.mCommentTextView!!.viewTreeObserver.removeOnGlobalLayoutListener( holder.commentTextViewOnGlobalLayoutListener) //add onGlobalLayoutListener holder.mCommentTextView!!.viewTreeObserver.addOnGlobalLayoutListener( if (holder.commentTextViewOnGlobalLayoutListener != null) holder.commentTextViewOnGlobalLayoutListener else CommentTextViewOnGlobalLayoutListener(holder, SpannableString(HtmlCompat.fromHtml( mView.getActivity(), commentDocument.html(), 0, null, SpanTagHandlerCompat(mView.getActivity())))))`
My onGlobalLayoutListener looks like this: `
class CommentTextViewOnGlobalLayoutListener( val holder: CommentAndFilesListViewViewHolder, val commentSpannable: Spannable) : ViewTreeObserver.OnGlobalLayoutListener { val LOG_TAG: String = CommentTextViewOnGlobalLayoutListener::class.java.simpleName override fun onGlobalLayout() { holder.mCommentTextView!!.viewTreeObserver.removeGlobalOnLayoutListener(this)
`
Well, that works. But how awful! Since I use the view holder template, I have to hold the variable in the listener and delete it if it has not been called and successfully deleted, because the onGlobalLayout function onGlobalLayout not called in time! And this is called too late, so you need to wait about 300 ms, and then look at the entire “reconstruction” of the TextView , and it looks disgusting!
So my question is: How do I make fields for the first n lines in a TextView before it is drawn on the user interface?