Friday, September 5, 2014

Xamarin.Android Custom View

Here is a simple outline for a custom view. I needed to create one for my own app and I didn't find all the info in one place. Hopefully this will help


The class definition

You must have at minimum the following in your class file. Then in the OnDraw method, tell it how to draw your view.

Adding your view to a layout

For the class attribute make sure that it is the namespace of the custom view in all lowercase. Then it is the class name of the custom view exactly as it is written in the class file. Example: As you can see below the class is "mynamespace.CustomView"

Events

You can wire up touch event listeners in the activity that shows the view with

3 comments:

  1. Aqui no debería ser "View" con mayúsculas?

    ReplyDelete
  2. Specifying the custom view class using the class attribute did not work for me, according to recent samples, this is the correct way:
    <mynamespace.CustomView android:id="@+id/MyCustomView" />

    ReplyDelete