2

I have in my LibGDX android application a table, below some textfields (one row) and a button below (Submit). My question is, how to "slide" up the table, textfiels and the button when the textfield is focuses (click on it) and the soft keyboard is appearing? In other words, how to make the soft keyboard not to cover my other objects. This solution might be related with this question: How can i find the height of soft-keyboard in a LibGDX application

Many thanks.

Community
  • 1
  • 1
JedyKnite
  • 71
  • 8
  • I never tryed it but try to set the `Gdx.gl.glViewport` from the softkey height to screen hight and the normal width. I am not sure, if the softkey uses the same viewport but if not this should be what you are looking for. – Robert P Jan 31 '14 at 07:10
  • Hi, I tried to figure out what at are you referring, but I failed :) Could you please give me a hint? – JedyKnite Jan 31 '14 at 17:59
  • I hope this helps you: http://www.acamara.es/blog/2012/02/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/. The "BlackBorders" should then be your Softkeyboard. – Robert P Feb 01 '14 at 11:15
  • Interesting reading, it will surely help me later for this application, but I didn't find any help there for this specific problem. Thanks anyway, as said the information from there will help me later. – JedyKnite Feb 01 '14 at 13:00
  • You have understood how to get hose blackborder right? If you resize the screen of your game, to fit over the softkeyborad the keyboard will fill the blackborder. At least i hope. It could be that the sofkeyboard belongs to the game and so it is shown in the resized windos. Take a look at this: http://stackoverflow.com/questions/21154157/android-livewallpaper-start-rendering-below-notification-bar/21156940#21156940. Thought the solution helpes also for this. – Robert P Feb 02 '14 at 13:59

3 Answers3

2

I found an workaround for my problem, it's working, but I don't think is too clean. So, my scenario was:

  • a container table, containing a ScrollPane (another table), under this scrolling table two TextFields and under them a Submit button
  • container table set to fill parent (full screen)
  • the text fields and the button aligned to the bottom of the container table

The problem was that when the OnscreenKeybord was visible, it covered the bottom objects (text fields and button) and a part of the scrolling table.

My solution / workaround is:

    keyboard = new OnscreenKeyboard() {
        @Override
        public void show(boolean visible) {
            Gdx.input.setOnscreenKeyboardVisible(visible);
            // Hmmmm...
            container.invalidate();
            if (visible) {
                // TODO get OsK height somehow!!!
                container.padBottom(310);
            } else {
                container.padBottom(0);
            }
        }
    };
    textFld1.setOnscreenKeyboard(keyboard);
    textFld2.setOnscreenKeyboard(keyboard);

and on the InputListener of the Submit button (on touchDown) I have a cleanup function like:

    textFld1.setText("");
    textFld2.setText("");
    stage.unfocus(textFld1);
    stage.unfocus(textFld2);
    keyboard.show(false);

In this way the container table will be padded up when the soft keyboard is displayed and it will be restored after the submit button is pressed; also the soft keyboard is hidden.

Now, there are two problems with this workaround:

  • I have not found a way to get the soft keyboard height; as you can see in the sample code I have chosen an arbitrary value for the up padding, so the tings to look good on my emulator

  • if hard button Back or ESC in emulator is pressed, the soft keyboard will hide, but the up-padding of the table will still be there; I didn't find how to determine if the soft keyboard is visible or not

If anyone finds another solution for my problem, please post it.

Thanks.

JedyKnite
  • 71
  • 8
1

i was totally wrestling with this problem too, but i think i found a really nice solution.

Solution in theory:

when the keyboard pops up, it makes the screen resize, thus calling libgdx's resize callback.

Advantages:

  • no need to have logic having to do with keyboard visibility detection
  • no need to get height of keyboard
  • handle everythin in the resize callback of the libgdx's screen class
  • simple to implement (i think)

Disadvantages

  • app can no longer be fullscreen; the status bar shows and things

How to do it:

  1. Create a main_layout.xml file, (....YourLibGdxThing\android\res\layout\main_layout.xml) such that has the following XML:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/main_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    
  2. modify your android launcher class (....YourLibGdxThing\android\src\your\name\space\android\AndroidLauncher.java) such that the onCreate looks like this:

    public class AndroidLauncher extends AndroidApplication
    {
        @Override
        protected void onCreate (Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_layout);
            AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
            FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main_frame_layout);
            frameLayout.addView(initializeForView(new YourGame(),config));
        }
    }
    
  3. change android:windowFullScreen attribute in the style.xml (....YourLibGdxThing\android\res\values\styles.xml) to false; the XML should look like this in style.xml:

    <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">false</item>    <!-- IMPORTANT -->
        </style>
    
    </resources>
    
  4. add the attribute android:windowSoftInputMode="adjustResize" to the android manifest, into the activity tag...my AndroidManifest (....YourLibGdxThing\android\AndroidManifest.xml) looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="my.project.reverse.domain.thing"
              android:versionCode="1"
              android:versionName="1.0" >
    
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="my.project.reverse.domain.thing.AndroidLauncher"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"       <!-- IMPORTANT -->
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
    </manifest>
    

Thanks! I hope it helps!!!!!!!!!!

Eric
  • 16,397
  • 8
  • 68
  • 76
-1

Set android:windowSoftInputMode to adjustPan in your <activity> block to get the result you want. See docs: https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Thanks for the fast answer, I tried your solution, but it doesn't seem to work with libGDX. The result is the same, when the textfield is clicked, the bottom objects are covered by the soft keyboard. – JedyKnite Jan 30 '14 at 21:01