adrianoubk
adrianoubk

Reputation: 225

LibGDX: setPosition() not working in Android

I'm trying to position a group of actors using the setPosition() method. I want to center the group in the screen continuosly while I'm adding actors, that is to say, I add one actor, the group is centered with one actor; I add a second actor, the group centers again but now with two actors, and so on. I use the next operation to do this:

Gdx.graphics.getWidth() - submitGroup.getMinWidth()) / 2 // Height is 0.

Group's minwidth is the sum of actors width. On desktop works perfect, but in Android doesn't make any effect and I don't know why. It doesn't center as I expected and I'm confused. Here is my code and two screenshots(desktop and android).

a.setSize(50, 50);
b.setSize(50, 50);
c.setSize(50, 50);
submitGroup = new HorizontalGroup();
submitGroup.addActor(a);
submitGroup.addActor(b);
submitGroup.addActor(c);
submitGroup.setPosition((Gdx.graphics.getWidth() - submitGroup.getMinWidth()) / 2, 0);
submitGroup.align(Align.bottom);
stage.addActor(submitGroup);

Desktop

Android

As it can see in the second one, the actors are not centered. My game is 800x480. What's going on? Any ideas?

Thanks in advance.

Upvotes: 0

Views: 1066

Answers (1)

adrianoubk
adrianoubk

Reputation: 225

Finally, I solved changing Gdx.graphics.getWidth() for 800. Why this?

Because I'm using a ScalingViewport of 800x480 and it's associated with my stage. So, this viewport shows the region as I indicated in the constructor, that is to say, I didn't create a ScalingViewport with screen's height and screen's width. Therefore, I have to take the viewport's size, not screen's size.

I hope this helps someone. I would appreciate any comments or improving this answer.

Upvotes: 0

Related Questions