0

When long-pressing an Entry on Android, the copy/paste context menu should appear by default. It works when the Entry is placed directly on the page. However, if the Entry is added to the window on a button click, the copy/paste context menu does not display, and the native Entry's ContextMenuCreated event is not triggered.

Sample Link

<VerticalStackLayout> <Button Text="Add entry to window" Clicked="OpenInWindow" HorizontalOptions="Fill" /> </VerticalStackLayout> 
 private void OpenInWindow(object sender, EventArgs e) { Grid grid = new Grid(); grid.BackgroundColor = Colors.LightGray; grid.HeightRequest = 100; grid.Padding = 100; Entry entry = new Entry() { BackgroundColor = Colors.Yellow, Text = "Entry", HeightRequest = 100 }; grid.Children.Add(entry); entry.Loaded += Entry_Loaded; #if ANDROID IWindowManager windowManager = WindowOverlayHelper.GetPlatformWindow()!.WindowManager!; this.GetWindowManagerLayoutParams(); if (windowManager != null && WindowManagerLayoutParams != null) { IMauiContext? context = grid.Handler?.MauiContext ?? WindowOverlayHelper.window?.Handler?.MauiContext; PlatformView childView = grid.ToPlatform(context); windowManager!.AddView(childView, WindowManagerLayoutParams); } #endif } private void Entry_Loaded(object? sender, EventArgs e) { #if ANDROID ((sender as Entry).Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ContextMenuCreated += MainPage_ContextMenuCreated; #endif } #if ANDROID private void MainPage_ContextMenuCreated(object? sender, PlatformView.CreateContextMenuEventArgs e) { System.Diagnostics.Debug.WriteLine("Context menu shown"); } public WindowManagerLayoutParams? WindowManagerLayoutParams; internal WindowManagerLayoutParams GetWindowManagerLayoutParams() { if (this.WindowManagerLayoutParams == null) { this.WindowManagerLayoutParams = new WindowManagerLayoutParams(); var decorViewContent = WindowOverlayHelper.decorViewContent; if (decorViewContent != null) { this.WindowManagerLayoutParams.Width = decorViewContent.Width; this.WindowManagerLayoutParams.Height = decorViewContent.Height; } this.WindowManagerLayoutParams.Format = Format.Translucent; } return this.WindowManagerLayoutParams; } #endif 
 public class WindowOverlayHelper { #region Fields /// <summary> /// Gets the application window. /// </summary> internal static IWindow? window => GetActiveWindow(); #if ANDROID public static PlatformView? decorViewContent => GetPlatformWindow()?.DecorView; #endif #endregion #region Private methods /// <summary> /// Helps to get the current active window. /// </summary> /// <returns> Current active window.</returns> private static IWindow? GetActiveWindow() { var windowCollection = (IPlatformApplication.Current?.Application as Microsoft.Maui.Controls.Application)?.Windows; if (windowCollection != null) { foreach (var window in windowCollection) { if (window != null) { var propertyInfo = window.GetType().GetProperty("IsActivated", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (propertyInfo != null) { var isActivated = (bool)(propertyInfo?.GetValue(window))!; if (isActivated) { return window; } } } } } return (IPlatformApplication.Current?.Application as Microsoft.Maui.Controls.Application)?.MainPage?.Window; } #if ANDROID /// <summary> /// Gets the activity window for Android. /// </summary> /// <returns>Returns the window of the platform view.</returns> public static Window? GetPlatformWindow() { if (window != null && window.Handler is WindowHandler windowHandler && windowHandler.PlatformView is Activity platformActivity) { if (platformActivity == null || platformActivity.WindowManager == null || platformActivity.WindowManager.DefaultDisplay == null) { return null; } return platformActivity.Window; } return null; } #endif #endregion } 

I checked if the context menu is created by verifying whether the ContextMenuCreated event is triggered using:

((Entry.Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ContextMenuCreated += MainPage_ContextMenuCreated; 

However, the event is not being triggered.

I verified that both the LongClick and Touch events are triggered for the Entry's native view.

Checked with .net maui Entry and Editor issue occurs in both.

5
  • Could you please show th full code? I got some errors when I copied your code in the OpenInWindow method. Such as no namespace about WindowOverlayHelper and this.GetWindowManagerLayoutParams(); Commented Jan 9 at 1:16
  • I have updated the code. Commented Jan 9 at 6:47
  • @LiyunZhang-MSFT I have included the complete code for your reference. I appreciate your assistance and look forward to your solution. Commented Jan 9 at 7:01
  • The public static PlatformView? decorViewContent => GetPlatformWindow()?.DecorView; give me the error: The type or namespace 'PlatformView' can't be found. How can you declare this variable?And it also shows the error in the OpenInWindow method. Commented Jan 9 at 8:28
  • @LiyunZhang-MSFT Please find the sample in the below link. Sample: drive.google.com/drive/folders/… Commented Jan 9 at 14:11

1 Answer 1

0

I cloned your project and reproduced your problem. And then I find the simialr issue in the native android:Android default cut/copy/paste not showing in EditText.

So I created a native android project with the Android Studio and added the EditText with the windowManager.AddView(). The same problem appeared.

After that, I found the issue in the Android Help Community: Copy-Paste option not appering on long press on Edit Text inside popup windows.

So this is an issue in the native android not the .net maui. You can report this in the Android Help Community. And for a workaround, you can try to add your own copy paste option to the entry or just give up the windowManager.AddView().

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.