Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How can I add a new property to a custom Xamarin.Android control in AXML?

ChrisSwainChrisSwain USMember, University ✭✭

I'm extending the TextView control in C# and want to add a new property called MaxTextSize and use that property in my AXML file. For some reason it is not recognized there. I can find the control in C# and set the property there, but not in the AXML file as an attribute. Here is the code snippet:

//Set the upper text size limit and invalidate the view
        public void SetMaxTextSize(float maxTextSize)
        {
            _maxTextSize = maxTextSize;
            RequestLayout();
            Invalidate();
        }

        //Return upper text size limit
        public float GetMaxTextSize()
        {
            return _maxTextSize;
        }

        public float MaxTextSize
        {
            get { return _maxTextSize; }
            set
            {
                _maxTextSize = value;
                RequestLayout();
                Invalidate();
            }
        }

And the AXML:

        <testtimer.android.controls.AutoScaleTextView
            p1:text="HH:MM:SS"
            p1:layout_width="wrap_content"
            p1:layout_height="wrap_content"
            p1:maxTextSize="1000dp"
            p1:minLines="1"
            p1:maxLines="1"
            p1:id="@+id/countdownText"
            p1:gravity="center"
            p1:layout_gravity="center_vertical" />

Best Answer

Answers

  • ChrisSwainChrisSwain USMember, University ✭✭

    Thanks Lori! Exactly what I needed.

    I wish there was a guide article on creating custom Xamarin.Android controls. Both from scratch and by inheriting from existing controls. Perhaps one day.

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    @ChrisSwain that's where blog posts come in :) I've been compiling a list of things to blog about based on the questions/issues that come up in the forums.

  • ChrisSwainChrisSwain USMember, University ✭✭
    edited September 2015

    @LoriLalonde - On a related note, is there a way to make the designer work with my new control? Right now it just shows a rectangle with the full namespace qualified control name as the text inside the control, and the designer says it is a control of type com.android.layoutlib.bridge.MockView.

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    I came across the same issue when working on a custom control but haven't found a way to resolve it. I assumed it's a limitation of the designer, but it will be interesting to see if there is a way to get that to work.

  • ChrisSwainChrisSwain USMember, University ✭✭

    Hopefully someone on the forums can confirm our suspicions that designer support isn't possible. Or, even better, tell us it is possible and show us how! :)

  • HowardBaylissHowardBayliss USMember ✭✭

    Thanks Chris; your blog post helped me.

  • NeedNapNeedNap ITMember

    Hi have a base customButton class with custom attributes, than a roundedButton and circleButton classes that extends from it.
    In Xamarin designers I can't see custom attributes :(.

    What can I do to solve this issue?
    Thanks

  • Great post. I am also hoping to get this working fully in the designer. If I figure it out, I'll be sure to share the solution.

Sign In or Register to comment.