sephiroth74 / android-target-tooltip

Create Toast like tooltips, but targets can be specified, plus custom properties and features
MIT License
1.49k stars 278 forks source link

Positioning Issue: Tip Window appear to be more bottom on Android 11 #150

Open samuelchou opened 3 years ago

samuelchou commented 3 years ago

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.

device-2020-12-21-150319

I'm sure that is not related to night theme, and not even related to gravity.

device-2020-12-21-151245

I don't know if this is related to Pixel (5) or Android 11, though.

samuelchou commented 3 years ago

using AVD Pixel 3a API 30 x86 can cause similar problem.

device-2020-12-21-154752

samuelchou commented 3 years ago

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)

amarilindra commented 3 years ago

Facing the same issue on Google Pixel 2 XL (Android 11)

Found any solution @samuelchou ?

SeanBlahovici commented 3 years ago

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
zarsky commented 3 years ago

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
    }
ankuratdunzo commented 1 year ago

@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