KennyAli
KennyAli

Reputation: 225

Change XML Background Programmatically - Xamarin.Android C#

I'm trying to change a Button background property programmatically using Xamarin.Android C# but can't get it done.

Xml file:

... <Button
                        p1:id="@+id/BtnGo"
                        p1:layout_width="wrap_content"
                        p1:layout_height="30dp"
                        p1:paddingHorizontal="15dp"
                        p1:text="Go"
                        p1:textColor="#ffffff"
                        p1:textSize="15dp"
                        p1:background="@drawable/togglefocus"
                        p1:textAllCaps="false"
                        p1:layout_gravity="center" /> ...

Need to change

p1:background="@drawable/togglefocus"

To

p1:background="@drawable/myfocus"

But on execution time / Programmatically (C#).

Tried this:

        Btn = FindViewById<Button>(Resource.Id.BtnGo);
        Btn.Click += delegate           
        {                
            Btn.Background("@drawable/myfocus"); //myfocus is an xml file in: Resources > Drawable
        };

Any Ideas?

Upvotes: 0

Views: 314

Answers (1)

FreakyAli
FreakyAli

Reputation: 16479

If I am not wrong you can use the SetBackgroundResouce method for this:

  Btn = FindViewById<Button>(Resource.Id.BtnGo);
    Btn.Click += delegate           
    {   
       Btn.SetBackgroundResource(Resource.Drawable.myfocus);
    };

Good luck! Feel free to get back if you have queries!

Upvotes: 1

Related Questions