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

  • Question

  • User125738 posted

    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" />

    Tuesday, September 1, 2015 4:48 PM

Answers

  • User48032 posted

    In your custom control, you need to declare styleable attributes for your custom properties within an attrs.xml file:

    <declare-styleable name="AutoScaleTextView">
        <attr name="maxTextSize" format="dimension" />
    </declare-styleable>
    

    This will allow you then to set the values within an AXML file in your main project.

    In this way when your control is loaded the attribute set is passed into the constructor. You just need to extract the attribute from that collection and set your property then.

    Something to this effect:

    TypedArray typeArray = context.ObtainStyledAttributes(attrs, Resource.Styleable.AutoScaleTextView, 0, 0);
    _maxTextSize = typeArray.GetDimension(Resource.Styleable.AutoScaleTextView_maxTextSize, 0);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 1, 2015 5:47 PM

All replies

  • User48032 posted

    In your custom control, you need to declare styleable attributes for your custom properties within an attrs.xml file:

    <declare-styleable name="AutoScaleTextView">
        <attr name="maxTextSize" format="dimension" />
    </declare-styleable>
    

    This will allow you then to set the values within an AXML file in your main project.

    In this way when your control is loaded the attribute set is passed into the constructor. You just need to extract the attribute from that collection and set your property then.

    Something to this effect:

    TypedArray typeArray = context.ObtainStyledAttributes(attrs, Resource.Styleable.AutoScaleTextView, 0, 0);
    _maxTextSize = typeArray.GetDimension(Resource.Styleable.AutoScaleTextView_maxTextSize, 0);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 1, 2015 5:47 PM
  • User125738 posted

    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.

    Tuesday, September 1, 2015 6:39 PM
  • User48032 posted

    @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.

    Tuesday, September 1, 2015 6:50 PM
  • User125738 posted

    @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.

    Wednesday, September 2, 2015 2:38 AM
  • User48032 posted

    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.

    Wednesday, September 2, 2015 12:55 PM
  • User125738 posted

    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! :)

    Wednesday, September 2, 2015 3:58 PM
  • User125738 posted

    Blogged about this and creating custom Xamarin.Android controls in general at http://www.solarityllc.com/blog/xamarin.android-custom-controls in case anyone is interested.

    Sunday, September 6, 2015 3:26 AM
  • User102055 posted

    Thanks Chris; your blog post helped me.

    Thursday, May 5, 2016 9:53 AM
  • User263735 posted

    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

    Friday, October 7, 2016 4:55 PM
  • User38688 posted

    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.

    Thursday, August 23, 2018 11:00 PM
  • Thursday, September 5, 2019 10:11 AM