Having a EditText over a soft keyboard with a GL View in the background programmatically
Asked Answered
C

2

8

I am working on a game and I want an EditText position above the soft keyboard when it pops up.

Here is what my code looks like when I build the View Hierarchy in my Main Activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

viewGroup = new RelativeLayout(this);
    viewGroup.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    view = new GameView(this);
        RelativeLayout.LayoutParams vLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        vLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        view.setLayoutParams(vLayoutParams);
        viewGroup.addView(view);

    editText = new GameEditText(this);
        RelativeLayout.LayoutParams etLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        etLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        editText.setGravity(Gravity.BOTTOM);
        editText.setLayoutParams(etLayoutParams);
    viewGroup.addView(editText);

setContentView(viewGroup);  

Here is my AndroidManifest.xml (the part the matters):

<activity 
    android:name="app.ember.MainActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:launchMode="singleTask"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="sensorLandscape"
    android:windowSoftInputMode="adjustResize"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Extra Information:

  • GameView = is a GLSurfaceView
  • GameEditText = is a EditText
  • If I comment out the first line (getWindow().addFlags...) Then the game view pans upward and the view isn't visible, unless I start typing stuff.
  • Leaving that first line in makes the game not pan, but doesn't move the editText field upward and the keyboard covers the editText field

Any suggestions?

Here are some fancy screenshots:

Game No Keyboard:

Game With No Keyboard

Game With Keyboard and WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS set (what it looks like in the monitor to show layer hierarchy):

Keyboard with monitor

Game With Keyboard and those flags NOT set. enter image description here

Here is the desired output: Desired Output

Consumption answered 17/12, 2014 at 23:34 Comment(4)
Could you not place the EditText in the center hidden, then show it when you show the keyboard? Leave the flags not set so it places the keyboard underneath the EditText.Weisman
@Dave S Thanks for the reply, Unfortunately I want the EditText to hug the top of the keyboard. Having the EditText in the center of the screen would work, but that is not the desired UI design, because it wouldn't look good on devices with more vertical space. I do have code that sets the EditText Object to be visible/invisible when the user needs to edit text in the game. The user does not tap into the EditText object in order to show the keyboard. Instead the user taps somewhere in the game and triggers to show the keyboard and then set the EditText to be visible and editable.Consumption
Well you can probably do manual adjustments based on the layout height changes when the keyboard shows. See this link for more: #16789459Weisman
@DaveS This doesn't seem to be working either. The dimensions are still the size of the whole screen. My current hypothesis is that the GL view is preventing the view from moving properly so it falls back to doing the pan.Consumption
H
-1

Put the Editbox as parent bottom.

android:layout_alignParentBottom="true"

and in the manifest file make the soft input adjustResize/Pan

android:windowSoftInputMode="adjustResize"

"adjustResize" : The activity's main window is always resized to make room for the soft keyboard on screen.

"adjustPan" : The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

Check this for more

Horatia answered 1/1, 2015 at 11:53 Comment(1)
if you look at my code, it is doing exactly this. Also my manifest is set to "adjustResize".Consumption
U
-1

In manifest add

android:windowSoftInputMode="adjustPan"

It will automatically adjust your keyboard below your edittext

Unconventionality answered 3/6, 2016 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.