SwitchCell does not look like as requested under Android

As shown in http://developer.xamarin.com/guides/cross-platform/xamarin-forms/user-interface/tableview/ link, we saw that a SwitchCell under Android does not show any text. This is what I want but when testing on my Nexus 7 tablet, I saw a OUI/NON label.

I used Xamarin studio 5.9.5 build 10 and Xamarin.Forms 1.4.4.6392.

I tried to code some renderers for Android with SwitchCell and Switch but I cannot success to call ShowText with false to hide this text...

I saw in forums that is a recurrent issue but I think it should be resolved for a long time. Just call in back end ShowText / false for Android !!!

In my company we wrote a cross-platform app and this problem seems to be critical.

Patrick Fradin

PS: I'm a French newly C#, Xamarin developer, so be cool with me ! ;-)

Best Answer

Answers

  • TorbenKruseTorbenKruse DEMember ✭✭✭
    edited August 2015

    I didn't test it myself. This doesn't work?

    public class CustomSwitchCellRenderer : SwitchCellRenderer
    {
        protected override global::Android.Views.View GetCellCore(Xamarin.Forms.Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
        {
            var cell = base.GetCellCore(item, convertView, parent, context);
    
            var swtchCell = cell as SwitchCellView;
    
            if (swtchCell != null)
            {
                var swtch = swtchCell.AccessoryView as global::Android.Widget.Switch;
    
                if (swtch != null)
                {
                    swtch.ShowText = false;
                }
            }
            return cell;
        }
    }
    

    If that really doesn't work, try this.

    public class CustomSwitchCellRenderer : SwitchCellRenderer
    {
        protected override global::Android.Views.View GetCellCore(Xamarin.Forms.Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
        {
            var cell = base.GetCellCore(item, convertView, parent, context);
    
            var swtchCell = cell as SwitchCellView;
    
            if (swtchCell != null)
            {
                var swtch = swtchCell.AccessoryView as global::Android.Widget.Switch;
    
                if (swtch != null)
                {
                    swtch.TextOn = string.Empty;
                    swtch.TextOff = string.Empty;
                }
            }
            return cell;
        }
    }
    
  • PatrickFradinPatrickFradin USMember

    Hi Torben,

    thanks for your answer. But your code does not work...

    Previously, I'd coded this

        protected override global::Android.Views.View GetCellCore (Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
        {
            ViewGroup view = base.GetCellCore(item, convertView, parent, context) as ViewGroup;
    
            Console.WriteLine("ImprovedSwitchCellRenderer -> GetCellCore - item=" + item + "Type=" + item.GetType() + " convertView=" + convertView + " parent=" + parent + " context=" + context);
    
            if (view != null)
            {
                Console.WriteLine("    Find Switch view with " + view.ChildCount + " children");
    
                for (int iChild = 0; iChild < view.ChildCount; ++iChild)
                {
                    var child = view.GetChildAt(iChild);
    
                    Console.WriteLine("       Child " + iChild + " Type=" + child.GetType());
    
                    if (child.GetType() == typeof(global::Android.Widget.Switch))
                    {
                        Console.WriteLine("           Switch = Child " + iChild);
    
                        var scView = view.GetChildAt(iChild) as global::Android.Widget.Switch;
    
                        // How to convert the child in Android Switch ?! :-(
                       scView.ShowText = false; // <== My code crashed here !!!!
                    }
                }
    
            }
    
            return view;
        }
    

    But my code crashed at the same line as yours...

    I don't understand what to do.

    Thanks again for your help

    Patrick

  • PatrickFradinPatrickFradin USMember

    Yes the first solution returns 'ERROR: setting SwitchCell ShowText: Exception of type 'Java.Lang.NoSuchMethodError' was thrown.'.

    The second one works well !!! Thank you very much. I will try to reduce the SwitchCell width because I think it's very large (like if default string remains).

    This is an acceptable workaround for me.

    Thank you again

    Patrick

  • PatrickFradinPatrickFradin USMember

    I have just added
    swtch.SwitchMinWidth = 0;

    To have the expected look (only a little squared check button)

    Patrick

Sign In or Register to comment.