A Xamarin/C# way:
var button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
button.Text = "Some New Text";
// If your image is a Drawable
button.SetBackgroundResource(Resource.Drawable.button_background);
// From an asset
button.Background = Drawable.CreateFromStream(Assets.Open("button_asset.jpg"), null);
// From an arbitrary path
button.Background = Drawable.CreateFromPath("/sdcard/Download/downloaded_file.jpg");
};
Update:
If you are using drawableLeft
, drawableRight
, etc… in your layout, you can change these via SetCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
:
button.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.button_background, 0);
If you are setting multiple drawables, you can preserve and change some of them via:
var drawables = button.GetCompoundDrawables();
button.SetCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], Resources.GetDrawable(Resource.Drawable.button_background, this.Theme), drawables[3]);
5
solved How to change an Image in a button on clicking the button?