1

A couple of firsts here - first Android app and first time using MonoDroid (I've got lots of experience with C# .NET).

In my user interface I want to draw a border around a TextView and found a post on SO (2026873) that recommended subclassing TextView. I also found another post (2695646) with some additional info on declaring a custom Android UI element using XML. (Note: All code in the example posts were in Java, had to translate into the C#/MonoDroid environment.)

When I run the code in the emulator I get a System.NullReferenceException: Object reference not set to an instance of an object.

Here's my out-of-the-box Activity1 code and the code for the subclassed TextView.

namespace MBTA
{
    [Activity(Label = "MBTA", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
        }
    }

    public class BorderedTextView : TextView
    {
        public BorderedTextView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { }
        public BorderedTextView(Context context, IAttributeSet attrs) : base(context, attrs) { }
        public BorderedTextview(Context context) : base(context) { }

        protected override void OnDraw (Android.Graphics.Canvas canvas)
        {
            base.OnDraw (canvas);

            Rect rect = new Rect();
            Paint paint = new Paint();

            paint.SetStyle(Android.Graphics.Paint.Style.Stoke);
            paint.Color = Android.Graphics.Color.White;
            paint.StrokeWidth = 3;

            GetLocalVisibleRect(rect);
            canvas.DrawRect(rect, paint);
        }
    }
}

My Main.axml layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/MBTA"     
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <MBTA.BorderedTextView
            android:text="DATE"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

And my attrs.xml file is as follows (with its BuildAction set to AndroidResource):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BorderedTextView">
        <attr name="android:text"/>
    <attr name="android:textSize"/>
    <attr name="android:layout_width"/>
    <attr name="android:layout_height"/>
    <attr name="android:gravity"/>
    <attr name="android:layout_weight"/>
    </declare-styleable>
</resources>

Thanks in advance.

Community
  • 1
  • 1
billmaya
  • 1,253
  • 3
  • 14
  • 34

1 Answers1

2

Mono for Android lowercases the namespace name when generating the Java Android Callable Wrappers for use by Android and layout XML files.

Consequently (as noted in the previous answer), you need to use mbta.BorderedTextView, not MBTA.BorderedTextView.

The Mono for Android documentation discuses using custom views in a layout, which addresses this scenario.

Community
  • 1
  • 1
jonp
  • 13,332
  • 4
  • 42
  • 60