How do I make a ProgressBar work in LibGDX?

adrianoubk picture adrianoubk · Jun 26, 2014 · Viewed 12.1k times · Source

I'm trying to understand how to use a ProgressBar in LibGDX.

I have created the bar but I don't know how to make it works. I want to duplicate the knob in order to fill the bar(background line) in 60 seconds. I know how to manage about the time, but there is no method in ProgressBar's class to fill the bar with the knob. At least, I haven't seen it (or I don't understand how, possibly). Here is my code:

ProgressBar's code:

skin = new Skin();
Pixmap pixmap = new Pixmap(10, 10, Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
skin.add("white", new Texture(pixmap));

textureBar = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("barGreen_horizontalMid.png"))));
barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
bar = new ProgressBar(0, 10, 0.5f, false, barStyle);
bar.setPosition(10, 10);
bar.setSize(290, bar.getPrefHeight());
bar.setAnimateDuration(2);
stage.addActor(bar);

I know that I can move the knob with the method setValue(float). But what I want is to fill bar with the knob's texture. Here is a bar's screenshot and the knob.

enter image description here

Can anyone help me to understand this? Thanks in advance.

Answer

user3920617 picture user3920617 · Aug 8, 2014

I was having the same problem, and finally found out.

You have to set a style for the knobBefore attribute to achieve what you want. Try this:

barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
barStyle.knobBefore = barStyle.knob;
bar = new ProgressBar(0, 10, 0.5f, false, barStyle);

Hope this helps!