Search code examples
javaioscodenameoneresourcebundlemaven-resources-plugin

CodenameOne - accessing images from nested folder hierarchy in app bundled resources


My CodenameOne app needs that some buttons have special icons. ScaleImageButton is the component that is suitable. The icons to be used are from the material icons repository, but some are modified, so the CN font is not suitable.

I created the icon images with the Flamingo tool that converts svg to java classes. It is cumbersome but it can be useful and practical, but at present time the resulting Java classes are not working on iOS.

It works on the CN simulator and Android although the button size is not exact: if state-changes reflect on some icon-changes, the layout would be rearranging and uneven.

On iOS, in addition to that, the drawing is huge or tiny in its viewport, depending of the button size that is assigned to the Flamingo image.

saveIconImage=new SaveIcon().scaled(normalButtonSize,normalButtonSize).toImage();
saveButton=new ScaleImageButton(saveIconImage);

There is an opened issue but the solution can take months or forever to come, if any, at least for such a niche issue.

So I decided to exploit the icon sets from the native Swift XCode version of my app I wisely created in parallel.

They are organized in nested subfolders (like in Assets.xcassets of XCode project), and when it comes to load resources files from the app bundle in CodenameOne, the path are special flat ones that do not like '/' or '.' chars.

Now they have a new maven building system but I do not know if something is changed about that.

So I ask whether it is possible or not to load images from folder hierarchy in the bundle and how it is achieved in terms of method calls and path strings.


Solution

  • The "right way" to do this is to add the images to the res file as that's the most portable way. You can do that via the theme or by including the images in your CSS, both options are discussed in the developer guide.

    You can also create a separate resource file for each icon set and you can give any name you want to the icons within.

    We don't support hierarchy for assets in Codename One. There's a special case for HTML which uses TAR to workaround platform differences but overall we don't allow that to avoid different behaviors between platforms.