Font Sizes in the attrs.xml File of a Xamarin.Android Compound Control

Nathan Sokalski 4,121 Reputation points

I am creating a compound control in Xamarin.Android compound control. Some of the properties that I need to include in attrs.xml are used to specify font sizes. I used format="float" for these properties in attrs.xml. In the Android Designer, this looks great and works fine when I use values like 22dp, but when I run the debugger, it complains about values with dp. However, using straightforward values like 22 do not display the correct size. What is the best way to include a property that will be used to specify a font size when creating a compound control?

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,311 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,476 Reputation points Microsoft Vendor


    Welcome to our Microsoft Q&A platform!

    If you want to set the fontsize with a float property. I used the following way.

    Firstly, I add a float property called fontScale in the attrs.xml.

       <?xml version="1.0" encoding="utf-8" ?>  
         <declare-styleable name="test">  
           <attr name="text" format="string" />  
           <attr name="testAttr" format="integer" />  
           <attr name = "fontScale" format = "float" />  

    Then I set it in the XML.


    We can get the value app:fontScale then set it to the custom control.

       public class MyTextView : TextView  
               protected MyTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)  
               public MyTextView(Context context) : base(context)  
                   public MyTextView(Context context, IAttributeSet attrs) : base(context, attrs)  
                   Initialize(context, attrs);  
               public MyTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)  
                   Initialize(context, attrs);  
               public MyTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)  
                   Initialize(context, attrs);  
               float ontScale;  
               private void Initialize(Context context, IAttributeSet attrs = null)  
                   if (attrs != null)  
                       TypedArray ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.test);  
                       string text = ta.GetString(Resource.Styleable.test_text);  
                       int textAttr = ta.GetInt(Resource.Styleable.test_testAttr, 0);  
                       ontScale = ta.GetFloat(Resource.Styleable.test_fontScale, 0);  
                       //set float value to the custom control‘s TextSize  
                       this.TextSize = ontScale;  

    Here is running screenshot.


    Best Regards,

    Leon Lu

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful