From aab7100fa9159ed5aa93d0921cd332be54ca96ea Mon Sep 17 00:00:00 2001 From: hunterstich Date: Mon, 11 May 2020 11:10:16 -0400 Subject: [PATCH] [Slider] Fix slider tooltip in popupwindows Slider needs to find an ancestor in which it can add its tooltip overlay. When added in a PopupWindow, its possible to search up the view hierarchy and find a parent which is neither a View nor a ViewGroup. This change adds a check for this case A note is that this change updates edge cases to return the current parent if it is any valid ancestor instead of just returning null. This allows Slider's tooltip to be added to Popup windows, but doesn't guarantee that the tooltip will be fully visible since a PopupWindow's height might be smaller than the slider + tooltip. If adding a Slider to a PopupWindow, you might need to manually add height to your popup's content view to account for the tooltip or turn of the slider's label. PiperOrigin-RevId: 310913776 (cherry picked from commit b631ef697984c695e1a16b1017b30f035a7599ce) --- .../android/material/internal/ViewUtils.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/java/com/google/android/material/internal/ViewUtils.java b/lib/java/com/google/android/material/internal/ViewUtils.java index 8a373b720..a5e543162 100644 --- a/lib/java/com/google/android/material/internal/ViewUtils.java +++ b/lib/java/com/google/android/material/internal/ViewUtils.java @@ -284,19 +284,24 @@ public class ViewUtils { /** Returns the content view that is the parent of the provided view. */ @Nullable public static ViewGroup getContentView(@Nullable View view) { - View parent = view; - while (parent != null) { - if (parent.getId() == android.R.id.content && parent instanceof ViewGroup) { - return (ViewGroup) parent; - } - if (parent.getParent() instanceof ViewGroup) { - parent = (ViewGroup) parent.getParent(); - } else if (parent.getParent() == null) { - // If android.R.id.content has not been found and parent has no more parents to search, - // exit. - return null; - } + if (view == null) { + return null; } + + View rootView = view.getRootView(); + ViewGroup contentView = rootView.findViewById(android.R.id.content); + if (contentView != null) { + return contentView; + } + + // Account for edge cases: Parent's parent can be null without ever having found + // android.R.id.content (e.g. if view is in an overlay during a transition). + // Additionally, sometimes parent's parent is neither a ViewGroup nor a View (e.g. if view + // is in a PopupWindow). + if (rootView != view && rootView instanceof ViewGroup) { + return (ViewGroup) rootView; + } + return null; }