How to close context action via code in xamarin forms

SupreetTare.9314SupreetTare.9314 GBUniversity ✭✭
edited December 2015 in Xamarin.Forms

I have created an app in xamarin forms, where I have used Context Actions in a list view item template's ViewCell as:

<ViewCell.ContextActions>
     <MenuItem Clicked="OnActiveJourneyEdit" 
                         Text="Edit"/>
     <MenuItem Clicked="OnActiveJourneyDelete" 
                         Text="Delete" 
                         IsDestructive="True" />
</ViewCell.ContextActions>

While I long press on any item in the list view I get context action items in action bar, and if I do not tap on any option in context action it will not be closed in android. Is there any way so that I can close that context action bar via code in xamarin forms?

Getting issue with context action on android, context menu is remained open while tapping on other buttons on the page, can anyone help to close the context action menus in xamarin forms.

Also please check the link for the same :

http://stackoverflow.com/questions/34057988/how-to-close-context-action-via-code-in-xamarin-forms

Answers

  • AndrewStephens.1111AndrewStephens.1111 USUniversity ✭✭

    Any update on this?

  • On Android, ContentAction are implemented with ActionModes and I don't see a way to close an action mode without having a reference to it. And, I don't see a way to get the action mode from our code since all of that is hidden inside the Android renderers. I also tried just programmatically clicking the back button since that closes the action mode but that did too much. It closed the aciton mode and also went back a page.

    So I don't think this is possible today.

  • MatheusSilvaMatheusSilva USMember ✭✭

    Hi @SupreetTare.9314, I have the same question, did you find the solution?
    Thank's

  • voidvoid DKBeta ✭✭✭
    edited November 2016

    I have this requirement as well. Especially on Android.

    Forms Team: Either you go modal and disallow any other UI events while in a context action state, or you give us a listview method to cancel the whole damn thing.

    iOS just works. As usual.

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    I can work on this tonight. I pushed a PR last night to fix another issue. I like the idea of having a ListView method to close context actions programmatically.

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    @AdrianKnight You're amazing - and your recent truckload of work on the XF system has been much appreciated.

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    @DavidDancy Thanks. :) Last night, I was able to handle the cases for iOS and Android, but for some reason UWP context actions didn't seem to be working (i.e. long pressing cells doesn't open up the context menu). I'm not sure if this is an existing issue.

  • AlejandroRuizAlejandroRuiz MXInsider ✭✭

    Hey I just found a way to achieve this on Android via dependency Service using FormsAppCompatActivity and Xamarin.Forms 2.5.0.91635

    1. Create a Dependency class

      public interface IContextManager
      {
           void CloseLastContextMenu();
      }
      
    2. Create Android Impl.

      [assembly: Xamarin.Forms.Dependency(typeof(ContextManager))]
      namespace YourNameSpace
      {
          public class ContextManager : IContextManager
          {
              public static Android.Support.V7.View.ActionMode LastActionMode { get; set; }
      
              public void CloseLastContextMenu()
              {
                  LastActionMode?.Finish();
              }
          }
      }
      
    3. Add next code into your MainActivity.cs

      public override Android.Support.V7.View.ActionMode StartSupportActionMode(Android.Support.V7.View.ActionMode.ICallback callback)
      {
                  return ContextManager.LastActionMode = base.StartSupportActionMode(callback);
      }
      
      public override void OnSupportActionModeFinished(Android.Support.V7.View.ActionMode mode)
      {
                  ContextManager.LastActionMode = null;
                  base.OnSupportActionModeFinished(mode);
      }
      
    4. Call the dependency impl.

      if(Device.RuntimePlatform == Device.Android)
          DependencyService.Get<IContextManager>().CloseLastContextMenu();
      
  • leeaptedleeapted Member

    @AlejandroRuiz Thanks. Your example really helped me.

    I had an issue involving a gesturerecognizer (tapped event) and a contextaction in a viewcell for a listview. Both were getting called on a long press. I adapted your code to create a dependency service that returns the status of actionmode and I check it in the tapped event handler.

Sign In or Register to comment.