Articles in this section
Category / Section

How to close the Picker on Android back button

5 mins read

You can close the Picker that is opened in the dialog mode when navigating back to another page by following the given steps:

Step 1: Add the necessary assemblies in the PCL, Android, iOS, and UWP projects. Refer to this UG documentation to know more about the assemblies required for adding SfPicker control to your project.

 

Step 2: Create a page with button inside it. On clicking the button, navigate to a new page that contains the SfPicker control.

 

Step 3: Create another page, in which add the SfPicker control with DialogMode. Add a new button, on clicking this, show the SfPicker using the IsOpen property.

 

On the App.cs page, use the following code.

 

C#:

public App ()
   {
 InitializeComponent();
 MainPage = new NavigationPage( new Sample.MainPage());
   }

 

Add the following code on the MainPage.

 

MainPage.xaml

 

<StackLayout>
        <Button Text="Click to Navigate" x:Name="click" />
</StackLayout>

 

MainPage.cs

namespace Sample
{
public partial class MainPage : ContentPage
{
 public MainPage()
 {
  InitializeComponent();
        click.Clicked += Click_Clicked;
 }
 
    async private void Click_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new ControlPage());
    }
}
}

 

On the new page, add a button and picker control as demonstrated in the following code.

XAML:

<ContentPage.Content>
        <Grid>
            <Button x:Name="control" Text="Open Picker"/>
            <syncfusion:SfPicker x:Name="picker" PickerMode="Dialog" />
        </Grid>
    </ContentPage.Content>

 

.CS Page:

 

namespace Sample
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ControlPage : ContentPage
    {
        protected override bool OnBackButtonPressed()
        {
            if (picker.IsOpen == true)
            {
                picker.IsOpen = false;
                return true;
            }
            else
                return false;
        }
        public ControlPage()
        {
            InitializeComponent();
            control.Clicked += Control_Clicked;
        }
 
        private void Control_Clicked(object sender, EventArgs e)
        {
            picker.IsOpen = true;
        }
    }
}

 

Please download the sample for closing the Picker on Android back button: Sample.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied