Skip to content

Commit 2b09956

Browse files
committed
StyledPlayerControlView: Fix view measurement issue
The exo_controls_background view is supposed to fill its parent, and so previously used match_parent to do this. However, if the parent uses wrap_content for its own dimensions, the constraints being specified become somewhat ambiguous. The parent is supposed to be sizing itself to wrap its children, and one of the children is supposed to be sizing itself to match the parent. Intuitively for this case, you'd hope that the layout logic would size the parent to wrap its other children, and that the match_parent child would then fill the parent with its determined size. That's not what happens, and instead the parent ends up expanding to occupy all of the space available to it. This commit sets the exo_controls_background view's dimensions to be 0dp in the layout, to stop it from influencing the size of the parent. It's then expanded to fill the parent in code. Issue: #8726 #minor-release PiperOrigin-RevId: 364868301
1 parent dc4148d commit 2b09956

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* Add group setting to `PlayerNotificationManager`.
1919
* Fix `StyledPlayerView` scrubber not reappearing correctly in some cases
2020
([#8646](https://github.com/google/ExoPlayer/issues/8646)).
21+
* Fix measurement of `StyledPlayerView` and `StyledPlayerControlView`
22+
when `wrap_content` is used
23+
([#8726](https://github.com/google/ExoPlayer/issues/8726)).
2124
* Audio:
2225
* Report unexpected discontinuities in
2326
`AnalyticsListener.onAudioSinkError`

library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerControlView.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,29 +1582,6 @@ private void onSettingViewClicked(int position) {
15821582
}
15831583
}
15841584

1585-
private void onLayoutChange(
1586-
View v,
1587-
int left,
1588-
int top,
1589-
int right,
1590-
int bottom,
1591-
int oldLeft,
1592-
int oldTop,
1593-
int oldRight,
1594-
int oldBottom) {
1595-
int width = right - left;
1596-
int height = bottom - top;
1597-
int oldWidth = oldRight - oldLeft;
1598-
int oldHeight = oldBottom - oldTop;
1599-
1600-
if ((width != oldWidth || height != oldHeight) && settingsWindow.isShowing()) {
1601-
updateSettingsWindowSize();
1602-
int xOffset = getWidth() - settingsWindow.getWidth() - settingsWindowMargin;
1603-
int yOffset = -settingsWindow.getHeight() - settingsWindowMargin;
1604-
settingsWindow.update(v, xOffset, yOffset, -1, -1);
1605-
}
1606-
}
1607-
16081585
@Override
16091586
public void onAttachedToWindow() {
16101587
super.onAttachedToWindow();
@@ -1676,6 +1653,35 @@ public boolean dispatchMediaKeyEvent(KeyEvent event) {
16761653
return true;
16771654
}
16781655

1656+
@Override
1657+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
1658+
super.onLayout(changed, left, top, right, bottom);
1659+
controlViewLayoutManager.onLayout(changed, left, top, right, bottom);
1660+
}
1661+
1662+
private void onLayoutChange(
1663+
View v,
1664+
int left,
1665+
int top,
1666+
int right,
1667+
int bottom,
1668+
int oldLeft,
1669+
int oldTop,
1670+
int oldRight,
1671+
int oldBottom) {
1672+
int width = right - left;
1673+
int height = bottom - top;
1674+
int oldWidth = oldRight - oldLeft;
1675+
int oldHeight = oldBottom - oldTop;
1676+
1677+
if ((width != oldWidth || height != oldHeight) && settingsWindow.isShowing()) {
1678+
updateSettingsWindowSize();
1679+
int xOffset = getWidth() - settingsWindow.getWidth() - settingsWindowMargin;
1680+
int yOffset = -settingsWindow.getHeight() - settingsWindowMargin;
1681+
settingsWindow.update(v, xOffset, yOffset, -1, -1);
1682+
}
1683+
}
1684+
16791685
private boolean shouldShowPauseButton() {
16801686
return player != null
16811687
&& player.getPlaybackState() != Player.STATE_ENDED

library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerControlViewLayoutManager.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
private final StyledPlayerControlView styledPlayerControlView;
5252

53+
@Nullable private final View controlsBackground;
5354
@Nullable private final ViewGroup centerControls;
5455
@Nullable private final ViewGroup bottomBar;
5556
@Nullable private final ViewGroup minimalControls;
@@ -99,7 +100,7 @@ public StyledPlayerControlViewLayoutManager(StyledPlayerControlView styledPlayer
99100
shownButtons = new ArrayList<>();
100101

101102
// Relating to Center View
102-
View controlsBackground = styledPlayerControlView.findViewById(R.id.exo_controls_background);
103+
controlsBackground = styledPlayerControlView.findViewById(R.id.exo_controls_background);
103104
centerControls = styledPlayerControlView.findViewById(R.id.exo_center_controls);
104105

105106
// Relating to Minimal Layout
@@ -464,6 +465,15 @@ private void setUxState(int uxState) {
464465
}
465466
}
466467

468+
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
469+
if (controlsBackground != null) {
470+
// The background view should occupy the entirety of the parent. This is done in code rather
471+
// than in layout XML to stop the background view from influencing the size of the parent if
472+
// it uses "wrap_content". See: https://github.com/google/ExoPlayer/issues/8726.
473+
controlsBackground.layout(0, 0, right - left, bottom - top);
474+
}
475+
}
476+
467477
private void onLayoutChange(
468478
View v,
469479
int left,

library/ui/src/main/res/layout/exo_styled_player_control_view.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
-->
1616
<merge xmlns:android="http://schemas.android.com/apk/res/android">
1717

18+
<!-- 0dp dimensions are used to prevent this view from influencing the size of
19+
the parent view if it uses "wrap_content". It is expanded to occupy the
20+
entirety of the parent in code, after the parent's size has been
21+
determined. See: https://github.com/google/ExoPlayer/issues/8726.
22+
-->
1823
<View android:id="@id/exo_controls_background"
19-
android:layout_width="match_parent"
20-
android:layout_height="match_parent"
21-
android:layout_gravity="center"
24+
android:layout_width="0dp"
25+
android:layout_height="0dp"
2226
android:background="@color/exo_black_opacity_60"/>
2327

2428
<FrameLayout android:id="@id/exo_bottom_bar"

0 commit comments

Comments
 (0)