Actually I met a problem with dynamically adjusting the textview gravity which depends on its line count.
I read this article, but I just don't really understand the difference about threading techniques, just like textview.post()
, runOnUiThread{}
, GlobalScope.launch(Dispatchers.Main)
, Thead{}.start()
.
TLDR, short question is "Why the codes below should be run in the post
scope?"
private fun setText(text : String) {
textView.text = text
textView.post {
if (textView.lineCount > 1) {
textView.gravity = Gravity.START
}else {
textView.gravity = Gravity.CENTER
}
}
}
and another short question is "Why it doesn't work in the GlobalScope.launch(Dispatchers.Main)?"
This is my try:
private fun setText(text : String) {
textView.text = text
GlobalScope.launch(Dispatchers.Main) {
if (textView.lineCount > 1) {
textView.gravity = Gravity.START
}else {
textView.gravity = Gravity.CENTER
}
}
}
I tried to put it into GlobalScope.launch(Dispatchers.Main), but it sometimes works and sometimes doesn't work.
Also, I tried to make it without textView.post()
, and it just doesn't work.
The source code of getLineCount()
doesn't seem to be async operation?
Any suggestion is welcome, thanks for everything.