Disable tabbed page swiping when using FormsAppCompatActivity

Hi, I am building an app in Xamarin Forms and have been using Xlab as the base class for the Main Activity on the Android side. I just switched to FormsAppCompatActivity as described in this link: https://developer.xamarin.com/guides/xamarin-forms/platform-features/android/appcompat/

By default the tabbed pages can be swiped. How do I disable swiping?

Best Answers

Answers

  • LushLush USMember ✭✭

    Hi @LazareenaThaveethu! Did you find a solution to this issue?

    Thanx!

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭
    edited August 2016

    HI @Lush !

    This solution worked for me.

    public class CustomTabbedRenderer : TabbedPageRenderer
    {
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {

            var info = typeof(TabbedPageRenderer).GetTypeInfo();
            // Disable animations only when UseAnimations is queried for enabling gestures
            var fieldInfo = info.GetField("_useAnimations", BindingFlags.Instance | BindingFlags.NonPublic);
    
            fieldInfo.SetValue(this, false);
    
            base.OnElementChanged(e);
    
            // Re-enable animations for everything else
            fieldInfo.SetValue(this, true);
        }
    }
    
  • BrightLeeBrightLee KRMember ✭✭✭

    @bunny-wabbit
    Hi, What if I want opposite?

    I want to enable swipe but don't want animation when I press tab because when I hit fourth tab from first tab is passes through every other tab and it makes app slow.

    Thanks.

  • Hello! While trying to use this code, i'm getting System.NullReferenceException: Object reference not set to an instance of an object. Any help?

  • krishnamvermakrishnamverma USMember ✭✭

    Hi All. I tried the above codes marked as answers to this question but still I am not able to disable the swipe gesture for tabbed pages in my application.
    I have created a tabbed page for which I have added a custom renderer in my Android project containing above code.
    I have recently updated all Xamarin components.
    I just wanted to ask if there anyone else also facing same issue or do I need to add something more which is not mentioned here. Or can the Xamarin update affecting this code?

  • EnriqueRangelEnriqueRangel USMember

    @krishnamverma, apparently with the last update, this code not work, at least on my app that code stopped working.

  • mwasimmwasim USMember ✭✭

    @EnriqueRangel said:
    @krishnamverma, to extend the previous comment, with the last xamarin forms update (2.3.4.231) to disable the swipe gesture in tabbed forms, I use this code and is working.

    using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
    
    namespace AppNamespace
        {
            public partial class MainPage : Xamarin.Forms.TabbedPage
            {
                public MainPage()
                {
                    InitializeComponent();
        
                    this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
                }
            }
        }
    

    Yes this solution works well on and above xamarin forms update (2.3.4.231).

  • yasiralijavedyasiralijaved PKUniversity ✭✭

    @EnriqueRangel said:
    @krishnamverma, to extend the previous comment, with the last xamarin forms update (2.3.4.231) to disable the swipe gesture in tabbed forms, I use this code and is working.

    using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
    
    namespace AppNamespace
        {
            public partial class MainPage : Xamarin.Forms.TabbedPage
            {
                public MainPage()
                {
                    InitializeComponent();
        
                    this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
                }
            }
        }
    

    This worked for me. Thanks

  • franoisLecointefranoisLecointe USMember ✭✭
    edited August 2017

    I work on Crossplatform and I also use syncfusion for a listview with a left / right swipe and the latter is in my tabbed (or the fact that I need to lock the tabbed swipe)

    I try the proposed solution, the worry is that, because I am on crossplatforms, there is an error on:
    public partial class MenuPrincipal : TabbedPage

    Indeed, he tells me that "TabbedPage is an ambiguous reference" the enbuggeée being done between the TabbedPage of Android and the TabbedPage of Forms.

    How can I remedy this? I have to make a renderer? If this is the case, I can not use this function (despite the tutorial)

    edit: Sorry, it's morning at home ^^'
    public partial class MenuPrincipal : Xamarin.Forms.TabbedPage
    quite simply

  • mandy0007mandy0007 INMember ✭✭

    If you wish to do it form code behind you can use following

    this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);

  • @BrightLee said:
    @bunny-wabbit
    Hi, What if I want opposite?

    I want to enable swipe but don't want animation when I press tab because when I hit fourth tab from first tab is passes through every other tab and it makes app slow.

    Thanks.

    Guys, any solution for this, please?

Sign In or Register to comment.