question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

SetBackgroundResource Not Working With Drawables

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?

dotnet-xamarin
5 |1600 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.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

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.



image.png (144.1 KiB)
image.png (142.0 KiB)
· 3
5 |1600 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 found my problem may be related to something slightly different. The Layout is used as the ViewHolder in a RecyclerView, so I obviously need to set the values of the TextView(s) inside the FrameLayout. In OnBindViewHolder, setting the Background of the FrameLayout works fine, and setting the Text of the TextView(s) works fine, but if I try to set both, the Text of the TextView(s) is not visible. Here is the ViewHolder code:

 public class PointsPerTurnViewHolder : RecyclerView.ViewHolder
 {
     public FrameLayout frmRoot { get; private set; }
     public TextView txtPoints { get; private set; }
     public TextView txtTotal { get; private set; }
     public PointsPerTurnViewHolder(View itemView) : base(itemView)
     {
         this.frmRoot = itemView as FrameLayout;
         this.txtPoints = itemView.FindViewById<TextView>(Resource.Id.txtPoints);
         this.txtTotal = itemView.FindViewById<TextView>(Resource.Id.txtTotal);
     }
 }
0 Votes 0 ·

I used this demo. https://docs.microsoft.com/en-us/samples/xamarin/monodroid-samples/android50-recyclerviewer/

And I set SetBackgroundResource for FrameLayout and TextView at the same time in the PhotoViewHolder.

frmRoot.SetBackgroundResource(Resource.Drawable.pointsperturndivider);
 Caption.SetBackgroundResource(Resource.Drawable.pointsperturndivider);


It worked as normal as this screenshot. TextView and FrameLayout have Background at the same time.

95195-image.png


0 Votes 0 ·
image.png (173.8 KiB)

May I know if you have got any chance to check my answer?

0 Votes 0 ·
njsokalski avatar image
0 Votes"
njsokalski answered LeonLu-MSFT commented

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

· 1
5 |1600 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.

Because it works in my side, could you share a demo about it?

0 Votes 0 ·