Search code examples
xamlmaui

.Net Maui Picker keeps reappearing after I select an item and tap anywhere else on screen


Here is the code, it is a simple picker and works well on latest versions of Android, but on 7.1 it bugs out

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Shell.NavBarIsVisible="false"
             x:Class="Views.Picker">
    <ContentPage.Content >
        <VerticalStackLayout Margin="25, 15">
            <Label Text="Device Picker" HorizontalTextAlignment="Center"/>

            <Grid>
                <Picker Grid.Column="0" SelectedItem="Dev" Margin="0,15,0,15" WidthRequest="200" HeightRequest="40">
                    <Picker.Items>
                        <x:String>Dev</x:String>
                        <x:String>Testing</x:String>
                        <x:String>Training</x:String>
                        <x:String>Production</x:String>
                    </Picker.Items>
                </Picker>

                <Image Source="small_down_arrow.png" 
                       Grid.Column="1"
                       x:Name="devicePicker"
                       WidthRequest="15" 
                       HeightRequest="15" 
                       HorizontalOptions="End"
                       Margin="0,0,6,0"
                   >
                </Image>
            </Grid>
        </VerticalStackLayout>
    </ContentPage.Content>
</ContentPage>

Below is an example of what is happening.

enter image description here

The closest issue I could find was this one:

Issue with Android MAUI .net picker - picker keeps reappearing

But in this case it he solved it by placing the picker in a CollectionView. Which as you can see in the code above will not work for me.


Solution

  • Alright, so seems the issue is this piece of code below, which I added at the very beginning of the project. I added it so that the Soft keyboard would not persist when a person navigate to a new page after they entered info for an input. I added it to Android on MainActivity.cs, removing the code fixes the issue but returns the persistent Soft Keyboard after navigation and input change.

    public override bool DispatchTouchEvent(MotionEvent e)
    {
        var view = CurrentFocus;
    
        if (e.Action == MotionEventActions.Down)
        {
    
            if (view is EditText editText)
            {
                editText.ClearFocus();
                InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
                imm.HideSoftInputFromWindow(view.WindowToken, 0);
            }
        }
    
        return base.DispatchTouchEvent(e);
    }