Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:11:24+00:00 2026-05-27T14:11:24+00:00

A couple of firsts here – first Android app and first time using MonoDroid

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:11:24+00:00Added an answer on May 27, 2026 at 2:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first winform app in .NET... I have made a couple of
First time posting on here, looked through a couple of possible similar titles, still
I'm in my first couple of days using Linq in C#, and I'm curious
I've realised for the first time a couple of weeks ago that when setting
first time posting in StackOverflow. :D I need my software to add a couple
newcomer and first ever question here. I am using the multiprocessing module of Python
I am working on my first mutlithreaded program and got stuck about a couple
I'm developing my first ASP.NET MVC 3 application and have a couple of jqGrid
This is my first time on here, so I'm bit nervous and please forgive
I have a couple of scripts for which the first part of them looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.