Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Switch control doesn't work in Android

ChristianSvrdChristianSvrd SEMember ✭✭✭

Hi, I'm trying to use a swtich control in android 4.1.2 but it doesn't seem to toggle no matter how I tap on it. it is enabled and visible, I tried it with binding and without neither help. (emulator)

Tagged:

Best Answer

  • ChristianSvrdChristianSvrd SEMember ✭✭✭
    Accepted Answer

    I found the issue, when the switch was inside a RelativeLayout parent it wasn't clickable, but when I moved it into a StackLayout it started to work. A Xamarin bug I guess.

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @ChristianSvrd - Post your code.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    try it on a real device. Switch works for everyone else. So its either your code or the fact that its an emulator.

    If you have a switch with no binding there shouldn't be anythign affecting it.
    If you have binding - it could be forcing it back to the same value all the time so it seems like its not working.

    As John said, need your markup and your code

  • ChristianSvrdChristianSvrd SEMember ✭✭✭
    Accepted Answer

    I found the issue, when the switch was inside a RelativeLayout parent it wasn't clickable, but when I moved it into a StackLayout it started to work. A Xamarin bug I guess.

  • JohnHardmanJohnHardman GBUniversity admin

    @ChristianSvrd - If you think you've found a Xamarin.Forms bug, please report it at bugzilla.xamarin.com so that Xamarin people know to investigate it.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    One quick check. Was the containing layout set to IsEnabled = false?
    It would be correct behavior to have nested controls disabled if the container is disabled.

  • JohnHardmanJohnHardman GBUniversity admin

    @ClintStLaurent - re. "It would be correct behavior to have nested controls disabled if the container is disabled."

    I suspect that's the behavior most of us would like. I even raised a bug for it a couple of years ago as it doesn't actually do that with Xamarin.Forms ( https://bugzilla.xamarin.com/show_bug.cgi?id=30384 ). Unfortunately, it was marked as not on roadmap.

    @ChristianSvrd - I happened to be doing some work with RelativeLayout today, so I popped a Switch into one to see what happened. Using XF 2.3.4, the Switch works fine inside RelativeLayout, so if you're using XF 2.3.4 it looks like the problem you were hitting was not related to the RelativeLayout.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JohnHardman said:
    I suspect that's the behavior most of us would like. I even raised a bug for it a couple of years ago as it doesn't actually do that with Xamarin.Forms ( https://bugzilla.xamarin.com/show_bug.cgi?id=30384 ). Unfortunately, it was marked as not on roadmap.

    The bugzilla I filed for it is marked "confirmed fixed"
    https://bugzilla.xamarin.com/show_bug.cgi?id=43007
    Then I reported it still existed as of 07july2017
    Then @EZHart reported that there is an additional issue with StackLayout ignoring the IsEnabled property of its children - and that has been fixed under another PR.

    To me - and according to that bugzilla - nested controls should now all behave as expected when nested and should enable/disable per the value of their container. In other words - Xamarin is saying that it should behave as we all expect: As you said you would expect.

    If you are finding that it isn't - then we need to get that bugzilla re-opened as not fixed.

  • JohnHardmanJohnHardman GBUniversity admin

    @ClintStLaurent - It's definitely not working in 2.3.4 stable - I've just set IsEnabled on a RelativeLayout to false, and the Switch inside the RelativeLayout still functions.

    I haven't tried the alpha channel - perhaps it is fixed there?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JohnHardman said:
    @ClintStLaurent - It's definitely not working in 2.3.4 stable - I've just set IsEnabled on a RelativeLayout to false, and the Switch inside the RelativeLayout still functions.

    I haven't tried the alpha channel - perhaps it is fixed there?

    Maybe you could add that confirmation of FAIL to the bugzilla?

    @ezhart - Since you marked that bugzilla as fixed maybe you can shed some like on this?

    I'm going to do some testing NOT in a Relative layout, but just some normal container nesting and see what happens.

  • JohnHardmanJohnHardman GBUniversity admin

    @ClintStLaurent - Done :-)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
        <Grid IsEnabled="False">
            <Label HorizontalOptions="Center"
                   VerticalOptions="Center"
                   Text="Welcome to Xamarin Forms!" />
            <Switch Margin="0,100" />
            <StackLayout Margin="0,200"
                         Orientation="Horizontal">
                <Label Text="Test" />
                <Switch />
            </StackLayout>
        </Grid>
    

    It doesn't get much simpler than that. One Switch inside a StackLayout and one not. Both still work.
    VS 2015u3 Enterprise Version 14..25431.01
    Xamarin extension: 4.6.0.299 - Latest stable, no updates available
    Xamarin.Forms 2.3.4.270
    Xamarin.Android.Support libraries: 25.4.0.2
    Java SKD 1.8.0_144
    There's not a thing on this PC that isn't version "V dot yesterday" up-to-date.

    @DavidOrtinau This is an example of what people are talking about when they say basic behavior bugs sit for year without being fixed. I opened this bug 13 months ago and a fix is still not in place in current stable release. Something as basic as a control nested in a grid doesn't inherit IsEnabled from the container. David, you said you were handling the bugzillas and new process. What can we do about this after it has been marked "resolved" but clearly is not? I'm not trying to rant - I'm trying learn how to handle this sort of thing so it get the man-hours it needs.

  • JohnHardmanJohnHardman GBUniversity admin

    @ClintStLaurent @ezhart @DavidOrtinau - On this one, although I also raised a bug against it 2+ years ago, I think the most important thing is that the actual behavior is documented.

    There's some overhead involved in recursing down (or up, depending on how it's implemented) the UI hierarchy to propagate the enabled/disabled state, so I can understand in a mobile environment why Xamarin might choose not to do this. But if it's not going to be done, the documentation for each type of View needs to clearly state the effect that IsEnabled has (or doesn't have). It might be worth marking IsEnabled deprecated where it doesn't have an effect, if that's going to remain the case.

  • ChristianSvrdChristianSvrd SEMember ✭✭✭
    edited August 2017

    @JohnHardman said:
    @ClintStLaurent - re. "It would be correct behavior to have nested controls disabled if the container is disabled."

    I suspect that's the behavior most of us would like. I even raised a bug for it a couple of years ago as it doesn't actually do that with Xamarin.Forms ( https://bugzilla.xamarin.com/show_bug.cgi?id=30384 ). Unfortunately, it was marked as not on roadmap.

    @ChristianSvrd - I happened to be doing some work with RelativeLayout today, so I popped a Switch into one to see what happened. Using XF 2.3.4, the Switch works fine inside RelativeLayout, so if you're using XF 2.3.4 it looks like the problem you were hitting was not related to the RelativeLayout.

    yes it wasn't just the relative layout alone that was causing the problem, it was in fact a combination of factors. the switch was inside an stacklayout that was also nested in a relative layout. I guess the bug was cos of switch componenet was rendered outside the bounds defined by RelativeLayoutConstraits but I'm not sure. Also It was working fine on iOS and Android >6

  • EZHartEZHart USXamarin Team Xamurai

    @ClintStLaurent said:

        <Grid IsEnabled="False">
            <Label HorizontalOptions="Center"
                   VerticalOptions="Center"
                   Text="Welcome to Xamarin Forms!" />
            <Switch Margin="0,100" />
            <StackLayout Margin="0,200"
                         Orientation="Horizontal">
                <Label Text="Test" />
                <Switch />
            </StackLayout>
        </Grid>
    

    This works as expected on master - neither Switch is enabled.

    Xamarin.Forms 2.3.4.270

    This fix post-dates 2.3.4; that's why it's in a later release: https://bugzilla.xamarin.com/show_bug.cgi?id=44096#c10

  • DavidOrtinauDavidOrtinau USForum Administrator, Xamarin Team, Insider, University Xamurai

    What can we do about this after it has been marked "resolved" but clearly is not?

    @ClintStLaurent are you saying "not" because the fix hasn't been included to date in a "stable" release? Or that even in master or -pre it's not fixed?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @DavidOrtinau said:

    What can we do about this after it has been marked "resolved" but clearly is not?

    @ClintStLaurent are you saying "not" because the fix hasn't been included to date in a "stable" release? Or that even in master or -pre it's not fixed?

    It was reported/marked fixed back in March in -pre6... But was not fixed: The problem was still reproducible. But marked 'Fixed'. It took 4 months of comments on that bugzilla before someone at Xamarin accepted that it really was still broken. That's very frustrating to people reporting bugs. Eventually someone confirmed it was a problem in the StackLayout: So more than one cause of the same symptom. I can't say that it really is fixed this time in a pre-release: I don't have the time to play beta tester at the moment.

    On the one hand we get told not to hammer and berate people for not responding - but sometimes it is only after hammering on people that someone looks into a bug. I don't like being a constant irritant. Moreover I don't like being made to be a constant irritant just to get someone to look at a bug report. Your customers shouldn't be put into that position.

    This is why my question is: What is the right way to address a bugzilla that is reported as fixed, when it really isn't fixed? How do we request someone re-examine/re-test a fix that we can show to to reproduce?

    As for the second part of your question; Because its hasn't been included in a stable release..
    Yeah, that's a problem. There are a great many people that have the freedom to just pull anything they like: Hurray for the kitchen-table whackers. But people in genuine professional enterprises can't do that. The corporate position is that we aren't here to be beta testers on pre-release versions. Corporate clients wait for official stable releases that have passed testing. So when stable releases keep getting pushed off to add more things its a problem.

    If stable releases could come out more often with less items it would be awesome. Isn't that something Xamarin even pushes as something your clients should do: Continuous Integration I think you call it. A new stable release every month that has 3 fixes in it. Instead of one release a year with 30.

    This works as expected on master - neither Switch is enabled.
    Xamarin.Forms 2.3.4.270
    This fix post-dates 2.3.4; that's why it's in a later release: https://bugzilla.xamarin.com/show_bug.cgi?id=44096#c10

    Thank you @EZHart - I look forward to someday actually getting it use it. But as long as it and all these other fixes I read about remain in prelease, there are tens of thousands of us that don't get the benefit of them.

    Just to sum it up:
    Reported 09/2016, now 09/2017 and a fix is still not released that the big-ticket Enterprise grade customers can use.

  • JulienRosenJulienRosen CAMember ✭✭✭✭
    edited September 2017

    it's absolutely gross that this is still not fixed.

    this is one of the very first issues i ran into years ago.

    come on guys.

    should things really be marked as "fixed" when they are months away from public consumption ??

  • Luca_BALuca_BA Member ✭✭

    I have the same experience here.
    i put inside a listview Template under a stackLayout within a GridLayout
    the Switch control is not clickable and does not even fire events .

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Luca_BA
    I'm pretty sure that works now. I'm doing it

    Tutorial
    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

  • Luca_BALuca_BA Member ✭✭

    have you tried it within stacklayouts in listview item as DataTemplate?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Luca_BA said:
    have you tried it within stacklayouts in listview item as DataTemplate?

    You saw the screenshot, right?

  • Luca_BALuca_BA Member ✭✭

    I'm really sorry for wasting ur time :(
    apparently the developer before me chose to set the Listview parent Stack "Enabled=false". No wonder why it did not work as it's supposed to

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Luca_BA said:
    I'm really sorry for wasting ur time :(

    No worries mate. Live happens.

    apparently the developer before me chose to set the Listview parent Stack "Enabled=false". No wonder why it did not work as it's supposed to

    Makes sense. Is there a DataTrigger in the XAML that re-enables it under specific conditions?

  • Luca_BALuca_BA Member ✭✭
    edited April 2019

    No , I think they just did it for LoLz :wink:
    ( my guess is just to make the click effect on the list item not show )
    Everythings works fine now that i set it to enabled=true. :smiley:

Sign In or Register to comment.