AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.23k stars 2.19k forks source link

Android soft keyboard opens on app start #13471

Open Adam-- opened 10 months ago

Adam-- commented 10 months ago

Describe the bug

Soft keyboard opens on app start on Android on our customized Android device.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new app from the Avalonia template.
  2. Deploy to our device, a custom ARM based Android board running a customized kiosk style Android 7.1 OS without a navigation or status bar.
  3. Notice the soft keyboard opens. Pressing enter doesn't close the keyboard.

I am unable to reproduce on an emulator or other physical devices.

Expected behavior

The soft keyboard doesn't open on app start.

Screenshots

image

Environment

Additional context

Please let me know if there is any additional information that I can provide to help with this issue.

timunie commented 10 months ago

A custom android setup? How should we debug it? Best bet is, you clone the source and debug it in src/samples/Sandbox on your special device. Maybe you find the root cause there.

Adam-- commented 10 months ago

A custom android setup? How should we debug it?

@timunie, I'd imagine there'd be some back and forth or some diagnostics logs that I could gather.

Best bet is, you clone the source and debug it in src/samples/Sandbox

I started there; however, when I build I get >1500 errors. Most of these errors seem to be related to types and namespaces not being found for XamlX, IXamlType, etc. I have the same issue in Visual Studio and Rider with Avalonia extension. I looked through the documentation and found no help for building the source there. Did I miss it? Is there something in particular I need install?

workgroupengineering commented 10 months ago

A custom android setup? How should we debug it?

@timunie, I'd imagine there'd be some back and forth or some diagnostics logs that I could gather.

Best bet is, you clone the source and debug it in src/samples/Sandbox

I started there; however, when I build I get >1500 errors. Most of these errors seem to be related to types and namespaces not being found for XamlX, IXamlType, etc. I have the same issue in Visual Studio and Rider with Avalonia extension. I looked through the documentation and found no help for building the source there. Did I miss it? Is there something in particular I need install?

Have you see this?

Adam-- commented 10 months ago

That's exactly what I was looking for, thanks!

Adam-- commented 10 months ago

Thanks again for linking the guide, I was able to follow along and I believe I have everything setup correctly. I don't get any issues building the core projects nor the wpf projects; however, when I attempt to build the MobileSandbox.Android project it gets stuck at:

12:22:19:777    1>Target "_GetPrimaryCpuAbi" in file "C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.95\tools\Xamarin.Android.Common.Debugging.targets":
12:22:19:777    1>  Task "GetPrimaryCpuAbi"

This never finishes and I have to cancel the build.

It's a similar experience in Rider.

It seems though that we are straying away from the original issue I had with the keyboard, would you like me to open another issue for this or is it okay to keep discussing here?

workgroupengineering commented 10 months ago

Check android workload is installed dotnet workload list

you should have a list like this:


Installed Workload Id      Manifest Version       Installation Source
---------------------------------------------------------------------
android                    33.0.68/7.0.100        VS 17.7.34031.279
wasm-tools-net6            7.0.11/7.0.100         VS 17.7.34031.279
wasm-tools                 7.0.11/7.0.100         VS 17.7.34031.279
maui-windows               7.0.92/7.0.100         VS 17.7.34031.279
maui-maccatalyst           7.0.92/7.0.100         VS 17.7.34031.279
maui-android               7.0.92/7.0.100         VS 17.7.34031.279
maccatalyst                16.4.7098/7.0.100      VS 17.7.34031.279
maui-ios                   7.0.92/7.0.100         VS 17.7.34031.279
ios                        16.4.7098/7.0.100      VS 17.7.34031.279

Use `dotnet workload search` to find additional workloads to install.
Adam-- commented 10 months ago

Here's mine:

Installed Workload Id      Manifest Version       Installation Source
-------------------------------------------------------------------------------
android                    33.0.95/7.0.100        SDK 7.0.400, VS 17.7.34221.43
ios                        16.4.7124/7.0.100      SDK 7.0.400, VS 17.7.34221.43
maccatalyst                16.4.7124/7.0.100      SDK 7.0.400, VS 17.7.34221.43
macos                      13.3.7124/7.0.100      SDK 7.0.400
wasm-experimental          7.0.13/7.0.100         SDK 7.0.400
wasm-tools                 7.0.13/7.0.100         SDK 7.0.400, VS 17.7.34221.43
maui-windows               7.0.100/7.0.100        VS 17.7.34221.43
maui-maccatalyst           7.0.100/7.0.100        VS 17.7.34221.43
maui-ios                   7.0.100/7.0.100        VS 17.7.34221.43
maui-android               7.0.100/7.0.100        VS 17.7.34221.43

Use `dotnet workload search` to find additional workloads to install.

Also, here's my Visual Studio configuration:

Microsoft Visual Studio Enterprise 2022 (2)
Version 17.7.6
VisualStudio.17.Release/17.7.6+34221.43
Microsoft .NET Framework
Version 4.8.09032

Installed Version: Enterprise

ASP.NET and Web Tools   17.7.274.37181
ASP.NET and Web Tools

AvaloniaPackage Extension   1.0
AvaloniaPackage Visual Studio Extension Detailed Info

Azure App Service Tools v3.0.0   17.7.274.37181
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   17.7.274.37181
Azure Functions and Web Jobs Tools

C# Tools   4.7.0-3.23517.17+9d4cc0304792762c34f41cbbec67a1fcd2dc764f
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.4.34 (main@d5ab18b)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub Copilot   1.129.0.0 (v1.129.0.0@14216f256)
GitHub Copilot is an AI pair programmer that helps you write code faster and with less work.

GitHub Copilot Agent   1.129.0

Microsoft Azure Tools for Visual Studio   2.9
Support for Azure Cloud Services projects

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio   17.7.27 (547ea6f)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   6.7.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Razor (ASP.NET Core)   17.7.3.2333001+0ab18affdf2a37647768d0e25f5f021bee6257a1
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   17.7.12.0
Microsoft SQL Server Data Tools

StylerPackage Extension   1.0
StylerPackage Visual Stuido Extension Detailed Info

Tweaks 2022   1.1.143
A collection of minor fixes and tweaks for Visual Studio to reduce the paper cuts and make you a happier developer

TypeScript Tools   17.0.20829.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.7.0-3.23517.17+9d4cc0304792762c34f41cbbec67a1fcd2dc764f
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android)   17.0.33906.96
Visual C++ for Cross Platform Mobile Development (Android)

Visual F# Tools   17.7.0-beta.23314.10+e612cf93b989503c89e3a5830090062b7ab5e143
Microsoft Visual F# Tools

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   17.7.0.223 (d17-7@c374b3e)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   17.7.3.9 (remotes/origin/d17-7@796d191def)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   17.7.21 (150ec9f)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   13.2.1.2 (d17-5/a8a26c7)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: d9a6e87
    Java.Interop: xamarin/java.interop/d17-5@149d70fe
    SQLite: xamarin/sqlite/3.40.1@68c69d8
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d

Xamarin.iOS and Xamarin.Mac SDK   16.4.0.18 (9d266025e)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
workgroupengineering commented 10 months ago
  1. Open Visual Studio
  2. Go Tools menu
  3. Go Android Menu

Check Adroid SDKs Manager thath Platforms and Tool are installed

Adam-- commented 10 months ago

Yes, they are installed. Specifically, I have

Platforms:

Tools:

Is there anything in particular that I'm looking for?

Adam-- commented 10 months ago

@workgroupengineering, I git clean -fxded and restarted my computer and I'm able to build and deploy the Sandbox and ControlCatalog Android projects now. Not sure why this fixed it. Thanks again for all your help!

Adam-- commented 10 months ago

I've stepped through the startup and I haven't been able to figure out why the keyboard is showing, but figured I'd share what I saw.

It doesn't seem that any of the code related to showing the Android keyboard in AndroidInputMethod is being called. SoftKeyboardListener does see that the keyboard is being opened.

Interestingly if I comment out the_host.FocusableInTouchMode = true in the AndroidInputMethod constructor and then set it after startup (very silly Task.Delay(10000).ContinueWith(task => _host.FocusableInTouchMode = true); in place of the _host.FocusableInTouchMode = true), the keyboard doesn't erroneously pop up and (after 10 seconds) works as expected. It's obviously not a solution to the problem, just an interesting observation. Also if I attempt to set FocusableInTouchMode = true in AvaloniaMainActivity InitializeApp or MainActivity OnCreate, they keyboard doesn't pop up even when it should.

Do you have any ideas on what I could look at or try from here?

maxkatz6 commented 10 months ago

cc @emmauss

Domik234 commented 5 months ago

I've stepped through the startup and I haven't been able to figure out why the keyboard is showing, but figured I'd share what I saw.

It doesn't seem that any of the code related to showing the Android keyboard in AndroidInputMethod is being called. SoftKeyboardListener does see that the keyboard is being opened.

Interestingly if I comment out the_host.FocusableInTouchMode = true in the AndroidInputMethod constructor and then set it after startup (very silly Task.Delay(10000).ContinueWith(task => _host.FocusableInTouchMode = true); in place of the _host.FocusableInTouchMode = true), the keyboard doesn't erroneously pop up and (after 10 seconds) works as expected. It's obviously not a solution to the problem, just an interesting observation. Also if I attempt to set FocusableInTouchMode = true in AvaloniaMainActivity InitializeApp or MainActivity OnCreate, they keyboard doesn't pop up even when it should.

Do you have any ideas on what I could look at or try from here?

I've got here randomly but it sounds like your background cannot have focus. On Android 7/7.1 there was a "problem" (it was not mistake) that there always had to be a focusable view and something had to be able to get the focus.

I would try to set "focusable" and "focusableintouchmode" for the background or call hiding the keyboard manually after appearing.

Also maybe this can help: SO

HideKeyboard example (I have this for XForms)

public void HideKeyboard()
{
    var activity = Platform.CurrentActivity;
    if (activity == null)
        return;

    var inputMethodManager = (InputMethodManager)activity.GetSystemService(AContext.InputMethodService);
    if (inputMethodManager == null)
        return;

    var token = activity.CurrentFocus?.WindowToken;
    if (token == null)
        return;

    inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
    activity.Window?.DecorView?.ClearFocus();
}