Skip to content

Transparent status bar color android#1630

Merged
ne0rrmatrix merged 16 commits into
mainfrom
vh/1587-transparent-status-bar-color-android
Aug 1, 2024
Merged

Transparent status bar color android#1630
ne0rrmatrix merged 16 commits into
mainfrom
vh/1587-transparent-status-bar-color-android

Conversation

@vhugogarcia
Copy link
Copy Markdown
Contributor

@vhugogarcia vhugogarcia commented Jan 2, 2024

Description of Change

Fixes the issue with the transparent color set on the status bar for Android. This is a draft, since I'm facing a weird issue on Android eventually when the status bar is set to transparent the Shell Navigation Title is moved some points down the screen. This error occurs on Shell and non-Shell pages in .NET MAUI 8.0.3. I'm not sure if this is an issue with the way we set the transparent color or if it is an issue on the .NET MAUI. I could not find a report of this issue on the MAUI repo.

Any tip or idea is always welcome! Thank you so much!

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated

Updated Sample

The sample has been updated to add support for Alpha values of the status bar.

Status-bar-transparent-edited.mp4

@vhugogarcia vhugogarcia requested a review from a team January 2, 2024 18:17
Comment thread src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs Outdated
Comment thread src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs Outdated
@dotnet-policy-service dotnet-policy-service Bot added stale The author has not responded in over 30 days help wanted This proposal has been approved and is ready to be implemented labels Apr 27, 2024
@vhugogarcia
Copy link
Copy Markdown
Contributor Author

vhugogarcia commented Jul 22, 2024

Hello @ne0rrmatrix, I remember you actually showed me how to solve the issue in Android when transparent status bar when the navbar goes down as you can see on the video demo below from second 20 to 30. However, I don't remember it, could you please give me a hand refreshing my brain on how to fix it? 😃

Status-bar-transparent-edited.mp4

Copy link
Copy Markdown
Member

@ne0rrmatrix ne0rrmatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace static void PlatformSetColor(Color color) with:

static void PlatformSetColor(Color color)
{
	if (IsSupported)
	{
		Activity.Window?.SetStatusBarColor(color.ToPlatform());
		if (Activity.Window is not null)
		{
			var platformColor = color.ToPlatform();
			Activity.Window.SetStatusBarColor(platformColor);
			var window = Activity.GetCurrentWindow();
			bool isColorTransparent = platformColor == PlatformColor.Transparent;
			if (isColorTransparent)
			{
				window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
			}
			else
			{
				window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
			}
			WindowCompat.SetDecorFitsSystemWindows(Activity.Window, !isColorTransparent);
		}
	}
}

@vhugogarcia
Copy link
Copy Markdown
Contributor Author

Thanks @ne0rrmatrix for sharing the insights. I made some improvements per our talk on Discord, and I believe this PR is now ready for review.

@pictos or @brminnick, could any of you give me a hand reviewing it? please 😃 This change is only for Android.

Screen.Recording.2024-07-22.at.10.36.22.a.m.mov

@vhugogarcia vhugogarcia marked this pull request as ready for review July 22, 2024 16:39
Copy link
Copy Markdown
Member

@ne0rrmatrix ne0rrmatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Let's wait for one more person to approve this.

pictos
pictos previously approved these changes Jul 31, 2024
Copy link
Copy Markdown
Member

@pictos pictos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good for me, just one request. I also notice that you changed the tabs to spaces, I believe we still have a bot that will do a PR fixing the code style, so no need to change here, but probably you would like to look why your IDE is doing that

Comment thread src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs Outdated
@vhugogarcia
Copy link
Copy Markdown
Contributor Author

Thanks @pictos for the review. The code has been improved and formatted. I'm using Visual Studio code and for formatting, I use the builtin with VS Code in macOS. I'm not sure if I have to select another one for better formatting in C#. I will do some research next time 😃

If the PR is approved, who can help me to merge it? 😃

Copy link
Copy Markdown
Member

@ne0rrmatrix ne0rrmatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@ne0rrmatrix ne0rrmatrix merged commit 636cf46 into main Aug 1, 2024
@ne0rrmatrix ne0rrmatrix deleted the vh/1587-transparent-status-bar-color-android branch August 1, 2024 18:26
Activity.Window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
}

WindowCompat.SetDecorFitsSystemWindows(Activity.Window, !isColorTransparent);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a check should be added here in case we already decided to SetDecorFitsSystemWindows to false (usually in MainActivity's OnCreate method) so it remains false, even if we decide to put a color to the status bar.

@plppp2001
Copy link
Copy Markdown

the video isn't working

@evgenyvalavin
Copy link
Copy Markdown

evgenyvalavin commented Oct 17, 2024

I'm facing a strange issue after the changes in this PR on Honor Play (COR-L29) Android API 29.

This code doesn't work with the changes in the PR since CommunityToolkit.Maui 9.0.3.

var listener = new WindowInsetsOldApiListener(activity.Window.DecorView);
activity.Window.DecorView.RootView?.ViewTreeObserver?.AddOnGlobalLayoutListener(listener);

With CommunityToolkit.Maui 9.0.2 the code above works.

I have this in MainPage.xaml

<Page.Behaviors>
    <mct:StatusBarBehavior StatusBarColor="Transparent" />
</Page.Behaviors>

bool isColorTransparent = platformColor == PlatformColor.Transparent;
if (isColorTransparent)
{
Activity.Window.ClearFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
Copy link
Copy Markdown

@evgenyvalavin evgenyvalavin Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point to call ClearFlags() if this call is followed by SetFlags() which replaces all the flags?
I think I have a bug because of this

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe instead if SetFlags() it should be AddFlags()

Copy link
Copy Markdown

@evgenyvalavin evgenyvalavin Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug i'm taking about #1630 (comment)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

help wanted This proposal has been approved and is ready to be implemented stale The author has not responded in over 30 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] StatusBarBehavior setting "StatusBarColor=Transparent" is not working [BUG] StatusBarBehavior transparent background when using .net maui shell

7 participants