Why is Android is not automatically scroll ViewElement into view when keyboard appear?

Severin Brinch Jørgensen 20 Reputation points
2023-02-02T06:53:20.55+00:00

In .net-Maui, When I add a ViewElement in a scrollView and the keyboard appear, the keyboard is hidding the ViewElement. If I remove the ScrollView it works fine, but then I can't scroll the content.

On Xamarin, Android will automatically scroll the ViewElement into view, so why has this changed?.

Do I have to do something different on Maui?

Here is an example of the code.

public class TestView : ContentView
    {
        public TestView()
        {
            var layout = new AbsoluteLayout();
            var scrollView = new ScrollView { Content = layout, };
            Content = scrollView;
            var entry = new Entry();
            layout.Add(entry);
            AbsoluteLayout.SetLayoutBounds(entry, new Rect(20, 600, 200, 45));
        }
    }

It makes no difference if I use another layout in the scrollview, I tried RelativeLayout and StackLayout, and it's the same issue

I would expect, that the ViewElement was automatically scrolled into view.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,838 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,021 Reputation points Microsoft Vendor
    2023-02-02T08:36:20.69+00:00

    Hello,

    Through code testing, I found that in MAUI, if the outer layer of your TestView is StackLayout, VerticalStackLayout and other layouts that are not inherited from ScrollView, this problem will occur. While in Xamarin.Froms, no matter what the outer layout is, the program layout can be adjusted normally when the soft keyboard pops up.

    For this problem, you can add the following code to the MainActivity to solve this problem:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Window.SetSoftInputMode(Android.Views.SoftInput.AdjustPan);
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful