Android - newTextField Cursor Position

Hi @nappa,

Can you please post your code? Last time I tested this, I recall that the cursor position works fine (except on the Corona Simulator, where it is noted in the docs that it does not).

Best regards,

Brent

Hi Brent, The following is a sample code.

local targetFontSize = 18 local fontSize = targetFontSize local fontScale = 1.0 if device.isGoogle then fontSize, fontScale = getAndroidFontSize(targetFontSize) end local fieldSize = getTextFieldHeight(fontName, fontSize) edtFld = native.newTextField( 0, display.topStatusBarHeight, display.contentWidth, fieldSize) edtFld.anchorX = 0.0 edtFld.anchorY = 0.0 edtFld.font = native.newFont( fontName, fontSize) edtFld.text = "" edtFld.inputType = "number" edtFld:setTextColor(1.0, 0.0, 0.0) edtFld.hasBackground = true edtFld:addEventListener( "userInput", function(event) if "began" == event.phase then elseif "ended" == event.phase then elseif "editing" == event.phase then local field = event.target local text = field.text local len = text:len() local edt = "" local cnt = 0 for i = 1, len do local dgt = text:sub(i, i) if "0" \<= dgt and "9" \>= dgt then if 0 ~= cnt and 0 == (cnt % 4) then edt = edt.." " end edt = edt..dgt cnt = cnt + 1 end if 16 \<= cnt then native.setKeyboardFocus(nil) break end end field.text = edt elseif "submitted" == event.phase then native.setKeyboardFocus(nil) end end) native.setKeyboardFocus(edtFld)&nbsp;

It works fine on ios and simulator. Corona SDK version is v2015.2530.

Hi @nappa,

I’m a little confused about your issue. You cite an issue with the cursor position, but I don’t see any usage of the setSelection() API in your code.

Brent

When a cursor is in a end of a text and the character is added in the code, even if setSelection() isn’t used, a cursor moves to an end automatically on simulator/ios. Why is behavior different on android?

Hi @nappa,

Can you please describe the intended behavior by your code? I could attempt to decipher what you’re trying to accomplish with all of the math, but I would appreciate if you can describe it first, then I can check out various aspects of the code block.

Thanks,

Brent

It is for input of the credit card number. An input number is separated by a blank each 4 digits, and if an input number reaches 16 digits, a keyboard focus comes off from a text field.

If necessary, I can upload main.lua which can work.

Thanks.

Hi @nappa,

I experimented with your code, and even tried to build my own short routine to handle the “spacing” between each set of 4 digits. In the end, this process gets very complex because, while I got the auto-spacing working nicely on the initial input, and upon deletion, it became difficult to manage this when the user de-focused the field and then returned and placed the cursor somewhere in the middle. I could continue working on this until I achieved perfection, but it would be fairly intensive and many conditions would need to be checked on every phase of the text entry.

Honestly, I think a better approach is to separate this input into 4 side-by-side text fields of maximum 4 digits, and when the user fills up one field, you set focus on the next one. This would keep everything very organized, and then when you accept submission of the “form” or whatever, you can concatenate all four fields together.

Brent

Hi Brent,
 

My client has decided this interface. And they’re satisfied about this behavior. In other words, they think this logic should move like on the ios. The reason that they adopted CoronaSDK is because application for multi-devices can be developed at the same time.

My hope is simple.  Why is behavior different on android?

iOS and Android are different beasts.  Apple and Google have their own way of doing things.  Corona Labs has a lot of control within our display technology, but when you get to items that start with native.* we become much more dependent on the OS provider on how things work.  It’s why they are called “native”.

If you want the cursor at the end of the string call:   textField.setSelection(10000,10000) and your cursor should stay at the end of the input string.  See:  http://docs.coronalabs.com/api/type/TextField/setSelection.html

Honestly if you want to do 4 chars, space, 4 chars, space, etc.  You might want to consider 4 text fields and when you hit 4 characters you jump to the end of the next field.  By hiding backgrounds of the text fields and clever spacing you can give it the illusion of what you’re going for.

Rob

OK. Thanks Rob.

If possible, Please also help about the following topic…
http://forums.coronalabs.com/topic/53599-error-received-authentication-challenge-is-null/#entry279178

I think I have the same problem using latest Corona SDK.

Hi @nappa,

Can you please post your code? Last time I tested this, I recall that the cursor position works fine (except on the Corona Simulator, where it is noted in the docs that it does not).

Best regards,

Brent

Hi Brent, The following is a sample code.

local targetFontSize = 18 local fontSize = targetFontSize local fontScale = 1.0 if device.isGoogle then fontSize, fontScale = getAndroidFontSize(targetFontSize) end local fieldSize = getTextFieldHeight(fontName, fontSize) edtFld = native.newTextField( 0, display.topStatusBarHeight, display.contentWidth, fieldSize) edtFld.anchorX = 0.0 edtFld.anchorY = 0.0 edtFld.font = native.newFont( fontName, fontSize) edtFld.text = "" edtFld.inputType = "number" edtFld:setTextColor(1.0, 0.0, 0.0) edtFld.hasBackground = true edtFld:addEventListener( "userInput", function(event) if "began" == event.phase then elseif "ended" == event.phase then elseif "editing" == event.phase then local field = event.target local text = field.text local len = text:len() local edt = "" local cnt = 0 for i = 1, len do local dgt = text:sub(i, i) if "0" \<= dgt and "9" \>= dgt then if 0 ~= cnt and 0 == (cnt % 4) then edt = edt.." " end edt = edt..dgt cnt = cnt + 1 end if 16 \<= cnt then native.setKeyboardFocus(nil) break end end field.text = edt elseif "submitted" == event.phase then native.setKeyboardFocus(nil) end end) native.setKeyboardFocus(edtFld)&nbsp;

It works fine on ios and simulator. Corona SDK version is v2015.2530.

Hi @nappa,

I’m a little confused about your issue. You cite an issue with the cursor position, but I don’t see any usage of the setSelection() API in your code.

Brent

When a cursor is in a end of a text and the character is added in the code, even if setSelection() isn’t used, a cursor moves to an end automatically on simulator/ios. Why is behavior different on android?

Hi @nappa,

Can you please describe the intended behavior by your code? I could attempt to decipher what you’re trying to accomplish with all of the math, but I would appreciate if you can describe it first, then I can check out various aspects of the code block.

Thanks,

Brent

It is for input of the credit card number. An input number is separated by a blank each 4 digits, and if an input number reaches 16 digits, a keyboard focus comes off from a text field.

If necessary, I can upload main.lua which can work.

Thanks.

Hi @nappa,

I experimented with your code, and even tried to build my own short routine to handle the “spacing” between each set of 4 digits. In the end, this process gets very complex because, while I got the auto-spacing working nicely on the initial input, and upon deletion, it became difficult to manage this when the user de-focused the field and then returned and placed the cursor somewhere in the middle. I could continue working on this until I achieved perfection, but it would be fairly intensive and many conditions would need to be checked on every phase of the text entry.

Honestly, I think a better approach is to separate this input into 4 side-by-side text fields of maximum 4 digits, and when the user fills up one field, you set focus on the next one. This would keep everything very organized, and then when you accept submission of the “form” or whatever, you can concatenate all four fields together.

Brent

Hi Brent,
 

My client has decided this interface. And they’re satisfied about this behavior. In other words, they think this logic should move like on the ios. The reason that they adopted CoronaSDK is because application for multi-devices can be developed at the same time.

My hope is simple.  Why is behavior different on android?

iOS and Android are different beasts.  Apple and Google have their own way of doing things.  Corona Labs has a lot of control within our display technology, but when you get to items that start with native.* we become much more dependent on the OS provider on how things work.  It’s why they are called “native”.

If you want the cursor at the end of the string call:   textField.setSelection(10000,10000) and your cursor should stay at the end of the input string.  See:  http://docs.coronalabs.com/api/type/TextField/setSelection.html

Honestly if you want to do 4 chars, space, 4 chars, space, etc.  You might want to consider 4 text fields and when you hit 4 characters you jump to the end of the next field.  By hiding backgrounds of the text fields and clever spacing you can give it the illusion of what you’re going for.

Rob