Xamarin.Android: Set Style in Codebehind

Nathan Sokalski 4,111 Reputation points
2021-03-03T00:07:37.623+00:00

How do I set the Style attribute of a View or Layout in codebehind? In xaml, it is simply set using the style attribute, but I cannot seem to find anything to set it in codebehind. In my specific case, I am trying to set the style of a GridLayout. Any ideas? Thanks.

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-03-03T07:12:37.76+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    For this, you can apply styles programmatically with custom view.

    1.You can define the attr of your control in file attrs.xml:

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

    2.And create a Custom view MyTextView as follows,and in function Initialize, you can set your style as you want.:

       public class MyTextView : TextView  
    {  
        protected MyTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)  
        {  
        }  
        public MyTextView(Context context) : base(context)  
        {  
            Initialize(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);  
                float ontScale = ta.GetFloat(Resource.Styleable.test_fontScale, 0);  
    
                var myColor = ContextCompat.GetColor(context, Resource.Color.yellow);  
    
                var testColor = ta.GetColor(Resource.Styleable.test_testColor, myColor);  
    
    
                this.TextSize = ontScale;  
    
                SetTextColor(testColor);  
                ta.Recycle();  
            }  
        }   
    }  
    

    3.Usage:

       <?xml version="1.0" encoding="utf-8"?>  
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:orientation="vertical"  
        android:layout_height="match_parent">  
    
        <CustomTextViewDemo.MyTextView  
            android:layout_width="100dp"  
           android:layout_height="100dp"  
           app:testAttr="600"  
           app:text="welcome"   
           app:fontScale="15"  
           android:text="test" >  
        </CustomTextViewDemo.MyTextView>  
    </LinearLayout>  
    

    Update:

    Take TextView for example, if you want to set style for it you can do like this:

    public void setTextAppearance(Context context, int resId)  
    {  
        if (Build.VERSION.SdkInt < BuildVersionCodes.M)  
        {  
            textView1.SetTextAppearance(context, resId);  
        }  
        else  
        {  
            textView1.SetTextAppearance(resId);  
        }  
    }  
    

    Note:the resId is the style id you define in file styles.xml

    Best Regards,

    Jessie Zhang


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.