Moving text field position on keyboard show in Lua for Android

Hello,

I was wondering if you could all assist me in the following problem I have been experiencing. I am in the process of designing an app with text fields towards the bottom of the screen, but I would like the text fields to change position once touched, so that the keyboard does not overlap them when it pops up.

As such, I created a listener to execute code which changes the Y position of 2 fields when either one is touched, but for some odd reason, the code is not working. If I place the code in a button event listener however, it seems to work fine. Please refer to my code:

----------------email textbox ------------- local textField = native.newTextField( display.contentCenterX, display.contentCenterY + 60, 200, 40 ) textField.placeholder = "Email" textField.isEditable = true --function to handle events local function touchListener( event ) textField.y = display.contentCenterY - 100 textField2.y = display.contentCenterY - 50 end textField:addEventListener( "touch", touchListener )

native.newTextFields’s are not display objects.  I don’t believe you can put a touch handler on them.  What you need to do, is in your began phase of your textField listener, move it to a visible area.  Then when the end phase triggers, move it back.

Here is the solution:

If I’m correct, since textFields are a native object (and not a display object), they do not handle “touch” event. As such you will need to use the “userInput” event to trigger the move of the textField.

Here is an example of the listener I have used in the past for this case (without the code to move around):

local function fctFieldListener(oEvent)
local oTextField = oEvent.target

if “began” == oEvent.phase then
– Move the input up if at the bottom
elseif “editing” == oEvent.phase then

elseif “submitted” == oEvent.phase then
native.setKeyboardFocus( nil )
elseif “ended” == oEvent.phase then
– Move the input back at his original place if adjusted
end
end

And you add it to the textfield like this:

oTextField:addEventListener( ‘userInput’, fctFieldListener )

native.newTextFields’s are not display objects.  I don’t believe you can put a touch handler on them.  What you need to do, is in your began phase of your textField listener, move it to a visible area.  Then when the end phase triggers, move it back.

Here is the solution:

If I’m correct, since textFields are a native object (and not a display object), they do not handle “touch” event. As such you will need to use the “userInput” event to trigger the move of the textField.

Here is an example of the listener I have used in the past for this case (without the code to move around):

local function fctFieldListener(oEvent)
local oTextField = oEvent.target

if “began” == oEvent.phase then
– Move the input up if at the bottom
elseif “editing” == oEvent.phase then

elseif “submitted” == oEvent.phase then
native.setKeyboardFocus( nil )
elseif “ended” == oEvent.phase then
– Move the input back at his original place if adjusted
end
end

And you add it to the textfield like this:

oTextField:addEventListener( ‘userInput’, fctFieldListener )