Search code examples
codenameone

How do I change the icon of back command?


I would like to change the size and icon of the back command by the codes below.

    Style style = UIManager.getInstance().getComponentStyle("h2b");
    FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MAP, style);
    back = new Command("",icon) {
        @Override
        public void actionPerformed(ActionEvent evt) {
            if (resetContent()){
                //removeMenu();
                showBack();
            }
        }
    };
    getToolbar().setBackCommand(back);

But the code doesn't work. the icon is always set to be MATERIAL_ARROW_BACK and its style is blue with a small font size.


Solution

  • Why not customize the BackCommand and TitleCommand UIIDs in the theme?

    This is preferable since by the time your code runs the style was already set and you would need to go back and edit it. Your approach is problematic since you set one icon. Overriding the style will make sure the icon remains consistent in pressed, disabled, etc. states.

    The reason this fails is because this code was designed to provide a unified navigation experience. It replaces the icon with a back arrow. If you insist on controlling this use:

    toolbar.setBackCommnand(cmd, BackCommandPolicy.AS_REGULAR_COMMAND);