tristan

Getting the height of Android device independent of pixel densities

Getting the height of Android device independent of pixel densities

Table of contents TLDR (too long didn't read) Why does this matter? Resources My app on the Google play store My app's GitHub code TLDR (too long didn't read) When trying to get the height of a Android device, make sure you do it in an pixel density independent way: correct way to get height: val displayMetrics = Resources.getSystem().displayMetrics val heightPixels = displayMetrics.heightPixels //exact physical pixel amount(different on certain devices) val density = displayMetrics.density //density multiplier val pixelDensityIndependentHeight =heightPixels / density Enter fullscreen mode Exit fullscreen mode val height = Resources.getSystem().displayMetrics.heightPixels //as previously mentioned, this height will change depending on…
Read More
Automatic retry function with Kotlin flows

Automatic retry function with Kotlin flows

Table of contents Short code example Why use this? My app on the Google play store Resources Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines. Chapter 10 Short code example Here is the code that will allow you to make automatic retries on a flow: fun <T, R : Any> Flow<T>.mapWithRetry( action: suspend (T) -> R, predicate: suspend (R, attempt: Int) -> Boolean ) = map { data -> var attempt = 0L var shallRetry: Boolean var lastValue: R? = null do { val tr = action(data) shallRetry = predicate(tr, (++attempt).toInt()) if (!shallRetry) lastValue = tr } while (shallRetry)…
Read More
No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.