Wednesday, December 16, 2015

libGDX Android Text Input

libGDX has its ups and its downs. It isn't perfect by any means, but I still believe it has a ton of potential especially with RoboVM at its side which makes it work wonderfully with iOS and still keeps it working great for Android and Desktop. I love the make it once, run it everywhere aspect that it provides. Also the same reason I love Java.

However the problem I was facing today was dealing with Android text input. I am amid creating my own library add-on for libGDX that provides a lot of the same GUI menu like items provided with Android, however they are directly for libGDX and use within games and game menus. I have been working on Buttons, Layouts, Text Inputs, Labels, Custom Widgets, Loading Bars, you name it. However when it came to text input from the soft-keyboard provided in Android, there were bugs that were all dependent on device, or if they weren't using Google keyboard. For a game that is supposed to run flawlessly on any device on which it can be installed, this wouldn't cut it. So I did a bit of popping around on the internet and came up with this way of creating a popup for Android that would take input.


Input.TextInputListener inputListener = new Input.TextInputListener() {
    @Override
    public void input(String input) {
        text = input;
    }

    @Override
    public void canceled() {

    }
};
             
Gdx.input.getTextInput(inputListener,  hint, text, "");

This worked excellently. It gave me a little popup that allowed for me to input text, then it would make the text in the widget, which I call EditText after Android EditText widget, change to be whatever I put in the popup. Everything was great. Just one problem. The popup looked almost 'prehistoric' or at least it was Android Froyo styling. Not exactly my taste. I like Lollipop much better, but I also supported back to Android Jelly Bean. So I did the next best thing.

In my Android part of the project I went into styles.xml and changed one part so that the android Dialog would look how I wanted it to.


Before:
<resources>
    <style name="GdxTheme" parent="android:Theme">
 <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


After:
<resources>
    <style name="GdxTheme" parent="android:Theme.DeviceDefault">
 <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


Changing the parent of GdxTheme from android:Theme to android:Theme.DeviceDefault will make it so that the popup matches whatever OS you're running, be it KitKat, JellyBean, or Marshmallow.

That's all for today. Hope this helps anyone else struggling with this problem.