Libgdx native TextInput limit text Length
I am writing a game using Libgdx. I used what was suggested here to handle the virtual keyboard when the user enters player names. It really works like a charm. However, if the user enters more than 8 characters in the name field, it breaks the UI design of my game. So I want to prevent the user from entering more than 8 characters.
TextField has a setMaxLength method as defined here . If I set this value to 8, no matter which user is logged in, the first 8 characters are placed in my textbox. But this is quite annoying and misleading because the user can still enter, say, 20 characters, having no idea that only the first 8 will be used.
So my question is if there is a mechanism to stop a user entering more than 8 characters even though I use a "native" way to handle TextField inputs.
Thanks in advance.
source to share
I only tested this before typing you, so I know it works. The code below will make your text field only allow 8 characters to be entered into the TextField widget. Be careful though some characters are larger (in length) than others (for example, short characters).
textField.setMaxLength(8); // Maximum chars will be 8
textField.setAlignment(1); // If you wanted to center the text
// (1 = Center, 2 = Right Align)
On the other hand, if you add a TextField to the table, you can change the visible width of the widget.
table.add(textField).width(50); // I believe this is in pixels
You didn't really give me many options (no code), so I hope this answer helps you.
We wish you good luck with your game!
source to share