-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fix for TimePicker Dialog doesn't update the layout when rotating the device with dialog open #31910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[Android] Fix for TimePicker Dialog doesn't update the layout when rotating the device with dialog open #31910
Conversation
Hey there @@HarishwaranVijayakumar! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could include a test?
|
||
void OnMainDisplayInfoChanged(object? sender, DisplayInfoChangedEventArgs e) | ||
{ | ||
TimePickerDialog? currentDialog = _dialog; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could include a null-check for _dialog
before attempting to use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsuarezruiz, I have made the suggested change.
@jsuarezruiz, The fix involves OnMainDisplayInfoChanged, which is only triggered when the device is rotated. However, SetOrientationLandscape does not actually rotate the device, so OnMainDisplayInfoChanged is not triggered, and the test still fails. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an issue where the TimePicker dialog on Android doesn't update its layout when the device is rotated while the dialog is open. The solution adds orientation change detection to automatically dismiss and recreate the dialog with the correct dimensions.
Key Changes
- Added orientation change detection via DeviceDisplay.MainDisplayInfoChanged event subscription
- Implemented dialog recreation when orientation changes to preserve user input
- Added proper lifecycle management for event subscriptions and dialog cleanup
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
TimePickerHandler.Android.cs | Added orientation change handling, dialog lifecycle management, and proper event subscription cleanup |
PublicAPI.Unshipped.txt | Added new public API entry for the ConnectHandler override method |
void ShowPickerDialog() | ||
{ | ||
if (VirtualView == null) | ||
if (VirtualView is null) |
Copilot
AI
Oct 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent null checking pattern. The existing code uses VirtualView == null
on line 75, but this change uses VirtualView is null
. For consistency, maintain the same null checking pattern throughout the file.
if (VirtualView is null) | |
if (VirtualView == null) |
Copilot uses AI. Check for mistakes.
if (_dialog is null) | ||
{ | ||
_dialog = CreateTimePickerDialog(hour, minute); | ||
} | ||
else | ||
{ | ||
// Update the dialog with new values (for orientation changes) | ||
_dialog.Dispose(); | ||
_dialog = CreateTimePickerDialog(hour, minute); | ||
} | ||
|
Copilot
AI
Oct 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic in both branches creates a new dialog. This can be simplified by disposing the existing dialog first (if it exists) and then creating a new one, eliminating code duplication.
if (_dialog is null) | |
{ | |
_dialog = CreateTimePickerDialog(hour, minute); | |
} | |
else | |
{ | |
// Update the dialog with new values (for orientation changes) | |
_dialog.Dispose(); | |
_dialog = CreateTimePickerDialog(hour, minute); | |
} | |
// Always dispose the existing dialog before creating a new one | |
_dialog?.Dispose(); | |
_dialog = CreateTimePickerDialog(hour, minute); |
Copilot uses AI. Check for mistakes.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Issue Details
Root Cause of the issue
Description of Change
Added ConnectHandler and related event subscriptions (ViewAttachedToWindow/ViewDetachedFromWindow) to manage display info changes and cleanup when the view is attached or detached. This helps ensure the time picker dialog responds to device orientation changes and releases resources properly.
Implemented OnMainDisplayInfoChanged to dismiss and recreate the time picker dialog with the current time when the device orientation changes, preserving user selection progress.
Issues Fixed
Fixes #31658
Reference
Tested the behaviour in the following platforms
Output
Screen.Recording.2025-10-08.at.4.50.08.pm.mov
Screen.Recording.2025-10-08.at.4.47.47.pm.mov