sanket
sanket

Reputation: 789

CodenameOne change button icon when pressed

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

Upvotes: 0

Views: 645

Answers (2)

Shai Almog
Shai Almog

Reputation: 52760

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

Upvotes: 0

Dylan Meeus
Dylan Meeus

Reputation: 5802

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();
    }
}

Upvotes: 1

Related Questions