Bug 7227 - Designer does not render custom views
Summary: Designer does not render custom views
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Android Designer ()
Version: 4.2.x
Hardware: PC Windows
: Normal enhancement
Target Milestone: 5.8
Assignee: Jérémie Laval
: 3384 ()
Depends on:
Reported: 2012-09-17 11:46 UTC by Goncalo Oliveira
Modified: 2014-12-08 08:36 UTC (History)
13 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Goncalo Oliveira 2012-09-17 11:46:23 UTC
I have the following custom layout

    public class SquareLayout : LinearLayout
        public SquareLayout( IntPtr javaReference, JniHandleOwnership transfer )
            : base( javaReference, transfer )

        public SquareLayout( Context context ) :
            base( context )

        public SquareLayout( Context context, IAttributeSet attrs ) :
            base( context, attrs )

When I add this layout to the axml, the VS designer is unable to render anything and I get the following error:

The layout could not be loaded: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup

The same thing happens if I try to use some of the support views from API 13, like android.support.v4.view.PagerTabStrip.
Comment 1 Alex P 2012-10-02 07:07:12 UTC
I have the same problem.
Comment 2 t9mike 2012-10-06 13:17:32 UTC
I think the ability to render and interact with custom views needs to be prioritized highly. I am implementing Android version of existing MonoTouch app, and right now this is my #1 issue with Mono for Android. The layout system in Android is super powerful, and I use designer much more in MfA than Xcode view editor with MonoTouch (where I did almost all view design in code). 

But with MfA, I am forced to not sub-class built-in views and view groups as much because I will loose all Designer support when I do so. But sub-classing is often a requirement to get some type of functionality that is not exposed publicly by Android class.

I think a basic short term solution could be to notice when a custom view is a sub-class of a native Android class, and simply interact with the native version in the Designer. So at design time no customizations, but at runtime instantiate the C# sub-class. At least for me, this would go a long way to reducing pain in this area.
Comment 3 t9mike 2012-10-06 13:22:17 UTC
Of course everything is ultimately a sub-class of View, so I suppose the rule would just be show/render super class that is in Android SDK.
Comment 4 Lluis Sanchez 2012-11-16 12:27:53 UTC
The cast exception is fixed.

Adding support for custom view rendering is not trivial since the designer views are rendered by the Android SDK, which runs on Java. I'll leave this bug report open as an enhancement.
Comment 5 t9mike 2012-11-16 12:39:11 UTC
I was thinking something like this could be done:

Before passing of layout file to Java for display, see if custom types found are .NET. If so, use reflection to determine parent class that is not custom. Then use temporary layout definition that replaces custom view with standard view so that it can be rendered.

But I have no knowledge of the tools/architecture. I don't think the priority should be LOW. This is a major stumbling block to using the Designer and can force certain architectural decisions (helper classes vs. sub-classing).
Comment 6 Lluis Sanchez 2012-11-28 10:03:52 UTC
I'll evaluate the viability of the option you propose.
Comment 7 Lluis Sanchez 2013-03-11 09:54:10 UTC
*** Bug 3384 has been marked as a duplicate of this bug. ***
Comment 8 Alex P 2013-05-03 11:25:47 UTC
Any news on this problem ?  This is really frustrating... we have a custom control wrapping all our view which make the designer useless.  When we want to use the designer, we need to replace our custom control by the base control ( LinearLayout ), use the designer, then replace base control with our control.
Comment 9 Lluis Sanchez 2013-05-06 10:12:45 UTC
No ETA for this yet. It's in the TODO list.
Comment 10 Flavius Demian 2014-08-15 14:11:44 UTC
I have a circular dependecy here in my init method on the CropImageView...maybe you could help me out. https://github.com/slown1/Xamarin.CircleImageCropper
Comment 11 Evan Harris 2014-11-06 05:44:31 UTC
This is a major annoyance for my team as we have complex views embedded in custom viewgroups. Switching our custom viewgroups out for standard android ones in order to manipulate the design is a daily gripe and something we would be very keen to see resolved in a future Xamarin release.
Comment 12 Jérémie Laval 2014-11-06 09:08:10 UTC
We will have a middle ground solution for this akin to what was described in comment 5 for our 5.7 release.
Comment 15 Jeremy Kolb 2014-12-07 18:38:18 UTC
Is that the next stable release?
Comment 16 Jérémie Laval 2014-12-07 18:41:43 UTC
This was actually pulled out of 5.7 because of other time constraining goals. It will be reinstated in 5.8 (which should hit stable roughly a month after 5.7).
Comment 17 Mohit Kheterpal 2014-12-08 08:36:20 UTC
As per comment 16, I am changing its target milestone as 5.8.