Question

I am setting an image as an icon to a button. I need to change the image when the icon is pressed. How can I achieve this in codenameone. BR, Sanket

Was it helpful?

Solution 2

Button has images for pressed/rollover/selected versions of icons.

OTHER TIPS

Do you mean whilst the button is pressed? As in, as long as it's held down and upon release restore the original image?

You could do this by creating your own button, which extends from CodenameOne's button. In this new button you can overwrite what "Pressed" and "Released" are supposed to do. Though keep in mind that releasing the button also has an action wired up to it, which restores the "not pressed" style.

For example

 public class NewButton extends Button
 {

public NewButton()
{
           super();
}

@Override
public void pressed()
{    
    super.pressed(); // To change the state of the button to pressed
    try
    {
        Resources r = Resources.open("/theme.res");
        Image pressed = r.getImage("bomb.png"); // Just an image I had in a project. 
        this.getStyle().setBgImage(pressed);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top