How to properly subclass SeekBar in MonoDroid?

I'm currently having a issue with the SeekBar class in MonoDroid.

Currently I have extended it like this:

 public class TTSeekBar : SeekBar, ITTComponent
    {
        public TTSeekBar(Context context): base(context)
        {

        }

        private int _min = 0;
        public int Min { get { return _min; } set { _min = value;} }

        public override int Progress
        {
            get
            {
                return base.Progress + _min;
            }
            set
            {
                base.Progress = value;
            }
        }

        public override int Max
        {
            get
            {
                return base.Max + _min;
            }
            set
            {
                base.Max = value + _min;
            }
        }

        public object GetInputData()
        {
            return (this.Progress + _min).ToString();
        }
    }

But whenever I try to create a object using TTSeekBar _seekBar = new TTSeekBar(this); (where this is a Activity) I get a Sytem.NotSupportedException thrown at the constructor with the message

Unable to activate instance of type TTApp.TTSeekBar from native handle 44fdad20

Extending other components of the Android.Widget namespace like this seems to work just fine, so I'm wondering why this one doens't work.

Answers


Just tested this on API Level 8 and it seems to work.

using System;
using System.Globalization;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using Android.OS;

namespace AndroidApplication1
{
    [Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

            var seekbar = new TTSeekBar(this);

            var ll = FindViewById<LinearLayout>(Resource.Id.LinearLayout);

            ll.AddView(seekbar);
        }
    }

    public class TTSeekBar : SeekBar
    {
        protected TTSeekBar(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public TTSeekBar(Context context) : base(context)
        {
        }

        public TTSeekBar(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        public TTSeekBar(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
        {
        }

        private int _min = 0;
        public int Min { get { return _min; } set { _min = value; } }

        public override int Progress
        {
            get
            {
                return base.Progress + _min;
            }
            set
            {
                base.Progress = value;
            }
        }

        public override int Max
        {
            get
            {
                return base.Max + _min;
            }
            set
            {
                base.Max = value + _min;
            }
        }

        public object GetInputData()
        {
            return (Progress + _min).ToString(CultureInfo.InvariantCulture);
        }
    }
}

So as I said you just need to implement the right constructors and it should work just fine.

There is an explanation as to why here: MonoDroid: Error when calling constructor of custom view - TwoDScrollView


Need Your Help

Excel VBA conditional formatting based on next cell value

excel vba excel-vba

I am trying to do some conditional formatting in Excel for the font size but seeing as it can't be done from the format menu, it needs to be done with VBA.

How can I submit a Sharepoint page and retain its state (prevent it from re-initializing)?

c# sharepoint-2010 form-submit submit-button

I have a Sharepoint web page that starts off with just a question to the user; once they answer it, the rest of the page is created. When the user submits the page via a "Save" button, the page rev...