SetBackgroundResource Not Working With Drawables

Nathan Sokalski 4,116 Reputation points
2021-05-06T21:05:15.967+00:00

I have the following two Drawables and Layout:

<?xml version="1.0" encoding="utf-8" ?>
<inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetLeft="2dp" android:insetTop="26dp" android:insetRight="2dp" android:insetBottom="26dp">
    <shape android:shape="rectangle">
        <size android:height="2dp"/>
        <solid android:color="@color/Black"/>
    </shape>
</inset>

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="fill" android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="28dp">
        <shape android:shape="rectangle">
            <size android:height="26dp"/>
            <solid android:color="@color/DarkOrange"/>
        </shape>
    </item>
    <item android:gravity="fill" android:left="2dp" android:top="26dp" android:right="2dp" android:bottom="26dp">
        <shape android:shape="rectangle">
            <size android:height="2dp"/>
            <solid android:color="@color/Black"/>
        </shape>
    </item>
</layer-list>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="0dp" android:background="@drawable/pointsperturndivider" tools:ignore="HardcodedSize">
    <TextView android:id="@+id/txtPoints"    style="@style/ItemTextView" android:layout_height="54dp" android:paddingBottom="27dp" tools:text="-2147483648"/>
    <TextView android:id="@+id/txtTotal" style="@style/ItemTextView" android:layout_height="54dp" android:paddingTop="27dp" tools:text="-2147483648"/>
</FrameLayout>

In my code, I want to dynamically set the Background property of the Layout to one of the Drawables using SetBackgroundResource. However, this does not seem to work. If I set android:background in the xml of the Layout, it works fine, but I need to change it in code. It also works fine when I use a Color resource instead of a Drawable resource. I also tried using Application.Context.GetDrawable to assign a value to the Background property, but this did not work either. How do I set the Background to a Drawable in code?

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

2 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 63,921 Reputation points Microsoft Vendor
    2021-05-07T02:33:33.383+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I make a test with FrameLayout, set the background resource dynamically with following code.

    I use following drawable called pointsperturndivider.xml

       <?xml version="1.0" encoding="utf-8" ?>   
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
            <item android:gravity="fill" android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="28dp">  
                <shape android:shape="rectangle">  
                    <size android:height="26dp"/>  
                    <solid android:color="@color/DarkOrange"/>  
                </shape>  
            </item>  
            <item android:gravity="fill" android:left="2dp" android:top="26dp" android:right="2dp" android:bottom="26dp">  
                <shape android:shape="rectangle">  
                    <size android:height="2dp"/>  
                    <solid android:color="@color/Black"/>  
                </shape>  
            </item>  
        </layer-list>  
    

    Then I set this drawable to FrameLayout after Button clicked

       public class MainActivity : AppCompatActivity  
           {  
               FrameLayout frameLayout1;  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
                   SetContentView(Resource.Layout.activity_main);  
                 
                   Button button1 = FindViewById<Button>(Resource.Id.button1);  
                   button1.Click += Button1_Click;  
                    frameLayout1 = FindViewById<FrameLayout>(Resource.Id.frameLayout1);  
                 
               }  
         
               private void Button1_Click(object sender, System.EventArgs e)  
               {  
                   // throw new System.NotImplementedException();  
                   frameLayout1.SetBackgroundResource(Resource.Drawable.pointsperturndivider);  
               }  
    

    Here is running screenshot.

    Before Button clicked.

    94592-image.png

    After Button clicked

    94586-image.png

    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.


  2. Nathan Sokalski 4,116 Reputation points
    2021-05-14T16:30:37.957+00:00

    Your code is pretty much exactly what I have, here is the exact line of code:

    pptvh.frmRoot.SetBackgroundResource(isselected ? Resource.Drawable.PointsPerTurnDividerSelected : Resource.Drawable.PointsPerTurnDivider);
    

    If I use either one of the above resources in the xml layout (and not in the codebehind), it works, but the codebehind does not work (it doesn't give an error, but it doesn't display the resource correctly).