android-target-tooltip
android-target-tooltip copied to clipboard
Positioning Issue: Tip Window appear to be more bottom on Android 11
Run the demo app on Google Pixel 5(Android 11) and you will see what similar to below. The ToolTip position seems to be more "bottom" than expected.
I'm sure that is not related to night theme, and not even related to gravity.
I don't know if this is related to Pixel (5) or Android 11, though.
using AVD Pixel 3a API 30 x86 can cause similar problem.
Android 8 and Android 10 cannot re-produce the issue, no matter what device it is. I guess the problem comes along with Android 11 (API 30)
Facing the same issue on Google Pixel 2 XL (Android 11)
Found any solution @samuelchou ?
So I banged my head against this issue, it seems to only be happening on Android 11 (not on Android 12).
Looks like you need to subtract the status bar height to get it aligned correctly.
val tooltip = Tooltip.Builder(context).anchor(this, 0, determineYOffset(context), true)
private fun determineYOffset(context: Context): Int =
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R)
-((context as? MainActivity)?.statusBarHeight ?: 0)
else
0
Also, it looks like if the device has a notch/cut out, you need to compensate for its height as well. Based on the example above:
private fun determineYOffset(context: Context): Int =
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
val activity = context as? MainActivity
val statusBarHeight = activity?.statusBarHeight ?: 0
val cutoutHeight = activity?.window?.decorView?.rootWindowInsets?.displayCutout?.safeInsetTop ?: 0
cutoutHeight - statusBarHeight
} else {
0
}
@zarsky When I tested out your method on a device with a notch the cutoutHeight
was equal to statusBarHeight
which made yOffset 0 again. Can you specify for which case you needed cutoutHeight as well?
Also, I got statusBarHeight using the following method.
val metrics: WindowMetrics = activity.windowManager.currentWindowMetrics
val insets = metrics.windowInsets.getInsets(WindowInsets.Type.statusBars())
val statusBarHeight = insets.top