A.J. Kolbasowski
A.J. Kolbasowski

Reputation: 23

Creating a TextField in LibGDX

I am working on an Android game using LibGDX and am trying to make a TextField to take in a player's name.This is what I have so far:

TextField.TextFieldStyle style = new TextField.TextFieldStyle();
    style.font = new BitmapFont();
    style.fontColor = Color.CHARTREUSE;

    TextField field = new TextField("", style);
    field.setText("Test");
    field.setWidth(150);

I then created a table, and added the TextField to it.

table.add(field).expandX().padTop(10);

    stage.addActor(table);

    Gdx.input.setInputProcessor(stage);

And this is what's in my render method:

 Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.draw();
    stage.act();

The only thing that appears is "Test", with nothing underneath. So the TextField is being created, but cannot take in any input. I have had trouble with Skins in the past, and didn't want to use one if I didn't have to. But I don't know if it's my lack of Skins that is the reason why it's not working, or if it is some other problem. Can someone please explain to me how to take in input in a TextField, or what my problem is?

Upvotes: 2

Views: 3142

Answers (2)

André Gourlet
André Gourlet

Reputation: 1

I know im a bit late to the topic but i had a similar problem and mine was fixed when i passed in a viewport to my stage. So make sure your stage has a viewport attached to it because it could be the error.

Stage = new Stage('''your viewport here''')

Upvotes: 0

TVASO
TVASO

Reputation: 501

I also had quite a few problems getting text input from users. I didn't find a way to use a TextView. If you want to get input from the user while using LibGDX, here's an easy way to do it:

Use a TextInputListener. You can read a tutorial on this page. It's not that beautiful looking, but it does the job just fine.

If you want the have a custom design, you shouldn't use the method above. When you want input from the user, check if a key is pressed (every frame). If so, append the typed character to a String. Render the String in a nice dialog using a BitmapFont.

Upvotes: 1

Related Questions