0

I am trying to disable touchscreen input for the user. This will be used to try and get a camera app to work underwater (where the phone is kept inside a diving case) to prevent conductive sea water touches.

I did come across suggestions on how to do it in android SDK:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

However, I cant seem to find ways to call this API in .NET MAUI. The only documentation I could find is this: https://learn.microsoft.com/en-us/dotnet/api/android.app.activity.window?view=xamarin-android-sdk-13

Which doesnt explain anything at all.

Zenovak
  • 25
  • 4

1 Answers1

2

You can try to put the following code in the Platforms\Android\MainActivity.cs's OnCreate method or the OnResume Method.

protected override void OnResume()
{
  base.OnResume();
  this.Window.SetFlags(Android.Views.WindowManagerFlags.NotTouchable,Android.Views.WindowManagerFlags.NotTouchable);
}

Or you can call this method in the share project. Such as:

public void DisableTouchable()
{
#if ANDROID
   Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Window.SetFlags(Android.Views.WindowManagerFlags.NotTouchable,Android.Views.WindowManagerFlags.NotTouchable);
#endif
}
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Thanks! This is one crucial part of a missing piece. Ive been scratching my head for days on how to get something even as simple as an activity context, cuz there's no way to call them in the MainPage.xaml.cs or anywhere else. Turns out It can be accessed via inheriting MauiAppCompatActivity. LOL – Zenovak Aug 01 '23 at 06:51
  • 1
    @Zenovak regarding *"Turns out it can be accessed via inheriting MauiAppCompatActivity"*: The most important point to understand, is that Android-specific code requires working with Android classes. This is most easily done inside files in folder Platforms/Android. There is already MainActivity.cs in that folder. MainActivity **is** the current activity (and is the "activity context"), in a Maui app on Android. Inside that class, `this` is all you need, to get the current activity context. [I just wanted to emphasize the overall point: look to the files in that folder, for Android code.] – ToolmakerSteve Aug 01 '23 at 08:04
  • Another useful tip: to find the c# equivalent of an android java method, search for it in xamarin android: search [xamarin android getwindow](https://www.google.com/search?q=xamarin+android+getwindow), scroll down a ways, see [Activity.Window property](https://learn.microsoft.com/en-us/dotnet/api/android.app.activity.window). – ToolmakerSteve Aug 01 '23 at 08:15