Skip to content

Commit a11e67f

Browse files
fenggaashok672
andauthored
Support Android edge to edge (#5499)
Adjust Android UI for 35 API to support edge to edge scenarios --------- Co-authored-by: Ashok Kumar Ramakrishnan <[email protected]>
1 parent 89b5a1d commit a11e67f

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/client/Microsoft.Identity.Client/Platforms/Android/EmbeddedWebview/AuthenticationAgentActivity.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,37 @@
1313
using Microsoft.Identity.Client.PlatformsCommon;
1414
using Microsoft.Identity.Client.PlatformsCommon.Shared;
1515
using Microsoft.Identity.Client.Utils;
16+
#if __ANDROID_30__
17+
using AndroidX.Core.View;
18+
using AndroidX.Core.Graphics;
19+
#endif
1620

1721
namespace Microsoft.Identity.Client.Platforms.Android.EmbeddedWebview
1822
{
1923
[Activity(ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, Exported=false)]
2024
internal class AuthenticationAgentActivity : Activity
2125
{
2226
private const string AboutBlankUri = "about:blank";
27+
private const int ApiLevelVanillaIceCream = 35; // Android API 35 (Vanilla Ice Cream)
2328
private CoreWebViewClient _client;
2429

2530
protected override void OnCreate(Bundle bundle)
2631
{
2732
base.OnCreate(bundle);
33+
34+
// Enable edge-to-edge for Android API 30+
35+
EnableEdgeToEdge();
36+
2837
// Create your application here
29-
3038
WebView webView = new WebView(this);
3139
var relativeLayout = new RelativeLayout(this);
3240
webView.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
3341

3442
relativeLayout.AddView(webView);
3543
SetContentView(relativeLayout);
44+
45+
// Apply window insets for edge-to-edge layout
46+
ApplyWindowInsets(relativeLayout, webView);
3647

3748
string url = Intent.GetStringExtra("Url");
3849

@@ -51,6 +62,36 @@ protected override void OnCreate(Bundle bundle)
5162
webView.LoadUrl(url);
5263
}
5364

65+
private void EnableEdgeToEdge()
66+
{
67+
#if __ANDROID_30__
68+
if (Build.VERSION.SdkInt >= BuildVersionCodes.R) // API 30+
69+
{
70+
// Enable edge-to-edge
71+
Window.SetDecorFitsSystemWindows(false);
72+
73+
// For API 35+, ensure proper edge-to-edge behavior
74+
if ((int)Build.VERSION.SdkInt >= ApiLevelVanillaIceCream)
75+
{
76+
// Additional API 35 specific configurations using SetStatusBarColor/SetNavigationBarColor methods
77+
Window.SetStatusBarColor(global::Android.Graphics.Color.Transparent);
78+
Window.SetNavigationBarColor(global::Android.Graphics.Color.Transparent);
79+
}
80+
}
81+
#endif
82+
}
83+
84+
private void ApplyWindowInsets(RelativeLayout parentLayout, WebView webView)
85+
{
86+
#if __ANDROID_30__
87+
if (Build.VERSION.SdkInt >= BuildVersionCodes.R) // API 30+
88+
{
89+
ViewCompat.SetOnApplyWindowInsetsListener(parentLayout, new OnApplyWindowInsetsListener(webView));
90+
ViewCompat.RequestApplyInsets(parentLayout);
91+
}
92+
#endif
93+
}
94+
5495
public override void Finish()
5596
{
5697
if (_client.ReturnIntent != null)
@@ -193,5 +234,38 @@ private void Finish(Activity activity, string url)
193234
activity.Finish();
194235
}
195236
}
237+
238+
#if __ANDROID_30__
239+
private class OnApplyWindowInsetsListener : Java.Lang.Object, AndroidX.Core.View.IOnApplyWindowInsetsListener
240+
{
241+
private readonly WebView _webView;
242+
243+
public OnApplyWindowInsetsListener(WebView webView)
244+
{
245+
_webView = webView;
246+
}
247+
248+
public WindowInsetsCompat OnApplyWindowInsets(global::Android.Views.View v, WindowInsetsCompat insets)
249+
{
250+
var systemBarsInsets = insets.GetInsets(WindowInsetsCompat.Type.SystemBars());
251+
var imeInsets = insets.GetInsets(WindowInsetsCompat.Type.Ime());
252+
253+
// Apply margins to avoid system UI overlap
254+
var layoutParams = _webView.LayoutParameters as RelativeLayout.LayoutParams;
255+
if (layoutParams != null)
256+
{
257+
layoutParams.SetMargins(
258+
systemBarsInsets.Left,
259+
systemBarsInsets.Top,
260+
systemBarsInsets.Right,
261+
Math.Max(systemBarsInsets.Bottom, imeInsets.Bottom)
262+
);
263+
_webView.LayoutParameters = layoutParams;
264+
}
265+
266+
return insets;
267+
}
268+
}
269+
#endif
196270
}
197271
}

0 commit comments

Comments
 (0)