Changing button color on mouse hover - LibGDX

I’ve searched all over and before I go about making a hoverlistener, I wanted to know if there was an easy way of changing the color of a button when a mouse hovers over it?

I have a TextButtonStyle which I pass to a TextButton. I noticed that the TextButtonStyle has a hover option but it is only for the font color. Are there any similar solutions which help change the overall color of a button on hover?

For anyone looking at this in the future, my current workaround is using a clickhandler, which has both enter/exit methods:

        
exitbutton.addListener(new ClickListener() {
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                if (!exitbuttonBool) {
                    exitbuttonBool = true;
                }
            }
            public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                if (exitbuttonBool) {
                    exitbuttonBool = false;
                }
            }
        });

I’m assigning a boolean to each button which keeps track of the code above. In the render method I have an updateUI() method which just goes down my list of booleans and checks their states. Inside the states I will set the colors manually:

button.setColor(f,f,f,f);

Here’s a variant which does not need manual traversal. I hard-coded the hover color for simplicity. It “exploits” the fact that during rendering, the UI updates the batch color with button.getColor().

	public static class RedColorOnMouseOverButton extends TextButton {

		private boolean hover;

		public MyButton(String text, Skin skin) {
			super(text, skin);
			addListener(new InputListener() {
				@Override
				public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
					hover = true;
				}

				@Override
				public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
					hover = false;
				}
			});
		}

		@Override
		public Color getColor() {
			if (!hover)
				return super.getColor();
			else
				return Color.RED;
		}
	}

Great idea! Thanks for sharing, I will most likely use something similar. I was hoping there was already a function for it, but it’s easy enough to do yourself.