question

njsokalski avatar image
0 Votes"
njsokalski asked ·

Xamarin.Android: Set Style in Codebehind

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.

dotnet-android
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered ·

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.







· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think you misunderstood my question. I have a style in the styles.xml file that I want to apply in codebehind.

0 Votes 0 ·

Hi @njsokalski ,I have updated my answer, you can check it at the bottom of my answer.Hope it can help you.

0 Votes 0 ·