Hi!
How do I set a backgroung image in an 'anchor pane' using CSS or any other way, I've search the net and tried many ways but I cannot get an image to show. This was my starting point:-
-fx-background-image:url("files/tweed.png");
Any help? Dave.
This code will set the background image to an asset file:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.Style = "-fx-background-image:url('" & File.GetUri(File.DirAssets, "smiley.gif") & "');"
MainForm.Show
End Sub
Add an ImageView to the anchorpane and put your image there.
Using anchorPane constraints (in the designer, layout) you can set the imageview to fill the anchorpane by putting all zeroes.
There seems to be a problem using a design-time style for images (i.e. setting it in the Scene Builder as one might usually hope). Things like background image URLs are "relative to the stylesheet's location" and I have no idea how you would put your images either "next to" the stylesheet or in a folder under the stylesheet's "folder" (wherever that is in a B4J program).
In scenebuilder Properties for the imageview under Specific, you can set the image directly where it says Image, click the ... box next to the field and a file dialog appears which should be pointing at the files folder of your project. Select the file from there.
That should give the same result as adding it in code with:
Hi!
Thanks for the replies I used Erel's solution and it works ok but only in 'debug' mode to get it to work in'release' mode i had to change it to :-
MainForm.RootPane.Style = "-fx-background-image:url('" & File.GetUri(File.DirApp, "smiley.gif") & "');" and put the .gif in the object folder.
I also thought it would work in the Scene Builder as '-fx-background-image:url("files/tweed.png");' but it did not.
Thanks again Dave.
The solution I posted should work in Release mode as well. Make sure that the file was added to the Files tab. I uploaded a very small project that you can try.
In scenebuilder Properties for the imageview under Specific, you can set the image directly where it says Image, click the ... box next to the field and a file dialog appears which should be pointing at the files folder of your project. Select the file from there.
That should give the same result as adding it in code with: