Skip to content

Commit

Permalink
don't reformat toast on pre-Lollipop
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Apr 28, 2024
1 parent 6291670 commit 89f09a0
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions app/src/main/java/ru/yourok/torrserve/app/AppToast.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yourok.torrserve.app

import android.os.Build
import android.text.TextUtils
import android.util.LayoutDirection
import android.view.LayoutInflater
Expand Down Expand Up @@ -77,25 +78,26 @@ class AppToast(
val appToast = AppToast(viewGroup, snackView)
appToast.setGestureInsetBottomIgnored(true) // don't add toolbar margin

tvMessage.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
tvMessage.viewTreeObserver.removeOnPreDrawListener(this)
// adjust tvMessage max lines to fit view
val fullLineHeight = tvMessage.lineHeight + tvMessage.getCompoundPaddingTop() + tvMessage.getCompoundPaddingBottom()
val noOfLinesVisible: Int = tvMessage.height / fullLineHeight
tvMessage.setMaxLines(noOfLinesVisible)
tvMessage.ellipsize = TextUtils.TruncateAt.END
// adjust appToast view height
val lineCount = tvMessage.lineCount
// Drawing happens after layout so we can assume getLineCount() returns the correct value
if (lineCount > 0) {
appToast.getView().updateLayoutParams {
height = 2 * paddingPx + fullLineHeight * lineCount
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
tvMessage.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
tvMessage.viewTreeObserver.removeOnPreDrawListener(this)
// adjust tvMessage max lines to fit view
val fullLineHeight = tvMessage.lineHeight + tvMessage.getCompoundPaddingTop() + tvMessage.getCompoundPaddingBottom()
val noOfLinesVisible: Int = tvMessage.height / fullLineHeight
tvMessage.setMaxLines(noOfLinesVisible)
tvMessage.ellipsize = TextUtils.TruncateAt.END
// adjust appToast view height
val lineCount = tvMessage.lineCount
// Drawing happens after layout so we can assume getLineCount() returns the correct value
if (lineCount > 0) {
appToast.getView().updateLayoutParams {
height = 2 * paddingPx + fullLineHeight * lineCount
}
}
return true
}
return true
}
})
})

return appToast
}
Expand Down

0 comments on commit 89f09a0

Please sign in to comment.