Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set the background of a button to be an image

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.

like image 614
GeekPeek Avatar asked Jan 20 '12 10:01

GeekPeek


People also ask

How to set background image for button in android programmatically?

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.

How to set image in button in android studio?

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.

How do you set a background image in RelativeLayout programmatically?

To set Background: RelativeLayout layout = (RelativeLayout) findViewById(R. id. background); layout. setBackgroundResource(R.

How to display image on button click in android studio?

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.


1 Answers

Try:
Button.Background = new ImageBrush{ ImageSource = new BitmapImage(new Uri(imgPath, UriKind.Relative)) };
like image 184
Divya Avatar answered Oct 12 '22 00:10

Divya