how can I programmatically set the background of a button to be an image? I know how to do it in XAML, but in code, I keep getting stuck, I tried
Button.Background = new ImageBrush{ ImageSource = "source" };
but then I get the error that string cannot be converted to ImageSource.
setBackgroundDrawable(new Button(this). getBackground()); ll. addView(btn); I have an image in path @drawable/new_todo_image to set as background for the button.
Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file.
To set Background: RelativeLayout layout = (RelativeLayout) findViewById(R. id. background); layout. setBackgroundResource(R.
You need to set the visibility of the ImageView such that it isn't shown at first either from java or from xml and then inside button action click listener you can change its visibility. This will make the ImageView invisible.
Try:
Button.Background = new ImageBrush{ ImageSource = new BitmapImage(new Uri(imgPath, UriKind.Relative)) };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With