Bug 33983 - Cannot override Layout method in EditText (or any TextView descendant)
Summary: Cannot override Layout method in EditText (or any TextView descendant)
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.1
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-09-15 15:13 UTC by Adam Kemp
Modified: 2015-09-24 12:02 UTC (History)
2 users (show)

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


Attachments
Example project (32.19 KB, application/zip)
2015-09-15 15:13 UTC, Adam Kemp
Details


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 for Bug 33983 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Adam Kemp 2015-09-15 15:13:12 UTC
Created attachment 12902 [details]
Example project

The TextView class in Xamarin.Android has a property Layout:

public new Layout Layout {
	[Register ("getLayout", "()Landroid/text/Layout;", "GetGetLayoutHandler")]
	get;
}

The "new" keyword is there to allow it to shadow this method in View:

[Register ("layout", "(IIII)V", "GetLayout_IIIIHandler")]
public virtual void Layout (int l, int t, int r, int b);

The problem is that this makes it impossible to override the Layout method from View in child classes of EditText (or other TextView descendants). For instance, this gives a compile error:

        private class MyEditText : EditText
        {
            public MyEditText(Context context) : base(context)
            {
            }

            public MyEditText(Context context, Android.Util.IAttributeSet attributes) : base(context, attributes)
            {
            }

            public override void Layout (int l, int t, int r, int b)
            {
                base.Layout (l, t, r, b);

                // Error CS0505: `TestEditTextLayout.MainActivity.MyEditText.Layout(int, int, int, int)': cannot override because `Android.Widget.TextView.Layout' is not a method (CS0505) (TestEditTextLayout)
            }
        }

While overriding Layout is somewhat uncommon, we were trying to do it in order to work around a bug in Android itself, and the Xamarin.Android C# wrapper's design was making this impossible. As far as I know there is no way to get the C# compiler to allow you to override this method.

If you decide not to fix this then I suggest being much more cautious in the future when considering using the "new" keyword.

Example project attached.
Comment 1 Jonathan Pryor 2015-09-15 15:54:23 UTC
Workaround: use [Java.Interop.ExportAttribute]:

https://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/#ExportAttribute_and_ExportFieldAttribute

For example:

    partial class MyEditText : EditText {
        [Export]
        public void Layout (int l, int t, int r, int b)
        {
            // ...
        }
    }
Comment 2 Adam Kemp 2015-09-15 15:57:41 UTC
Thanks! I figured there must be some Xamarin.Android runtime workaround.