Android - newTextField Cursor Position

Hello, I am currently trying to move the cursor to the last position on the newTextField…

What i am doing is dynamically adding text to the field, but if the user tries to enter any text after it, it does not place the cursor at the end… any suggestions?

anyone have any idea? corona staff?

Hi @eja,

Which version of Corona are you using? Can you post your code?

Thanks,

Brent

Hi Brent, I am using the latest build (CoronaSDK 2014.2238). This is the most basic version I can write/think of:

local widget=require("widget") local textField local function addText(e) textField.text=textField.text.."testing" return true end textField=native.newTextField(display.contentWidth\*0.5,60,200,45) textField.font=native.newFont(native.systemFont,18) textField:setTextColor(0.5) textField.inputType="default" local addButton=widget.newButton{ width=200,height=45,left=60,top=100, defaultFile="button.png",overFile="button-over.png", font=native.systemFont,fontSize=14,label="Add Text to Field", labelColor={default={0},over={0}},onRelease=addText } timer.performWithDelay(1,function() native.setKeyboardFocus(textField) end)

If you try this code using the xcode simulator (ios device), you will notice after you type anything and then click the button, the cursor will always be placed at the end (adding to the field).

However , with Android, it will not, it will remain where you last typed.

Any techniques you can think of Brent?

No idea of your use case, but perhaps if you add/change the text inside the editing handler,  the cursor will set properly. However, it’s not very likely your app works this way. ie; if you ad/remove text based on user input inside the field, you could change the text during the handler call, and it should work.

Another strategy to work around the problem could be to destroy and recreate the field with the new text when you need to change the text on the fly, quickly.

Just a couple thoughts.

@mpappas sadly, the text thats being added is not based on user input (user is selecting friends list).

I did try your other strategy hoping for success and well… it didn’t work :frowning: What it actually did was place the cursor at the beginning (even tried using setKeyboardFocus after w/ a timer too, and still the cursor was at the beginning).

any more suggestions?

I am creating a text field with text in it (login field) in my app, and the cursor appears at the end of the text string… (android 4.3, latest SDK)…

It works for me in my app.

Can you post your new test code, or your actual app code that failed when setting the text first?


EDIT: Also, my field creation sets a field called “isEditable”, as below

[lua]

       editTextField = native.newTextField( tempX, tempY, tempW, tempH, keyboardHandlerEditText)
        editTextField.font = native.newFont( native.systemFont, tempSize )
        editTextField.inputType = keyboardType
        editTextField.align = ‘left’;
        editTextField.text = editText                   – Passed in text argument
        editTextField.isEditable = true               – allow user to edit
        editTextField:setTextColor( 0,0,0 )        – Native field apparently still uses setTextColor, not setFillColor after g2.0 release…
        editTextField.hasBackground = false     – true     – use true for debugging position of field
        editTextField.isSecure = isSecure

        native.setKeyboardFocus( editTextField )  – Get the keyboard coming onscreen…

        print("  editTextField created – textSize ==", tempSize)
        print(" x,y == ", editTextField.x, editTextField.y)

[/lua]

@mpappas you know, i think it was the fact I was using a timer around native.setKeyboardFocus (not even sure why i did). With a few modifications, this method for a fix you mentioned now is working, thanks bud. Posting for future reference:
 

Ultra stripped down working version for Android cursor position fix:

display.setDefault("background", 0.5) local widget=require("widget") local textField local function createField() local field=native.newTextField(display.contentWidth\*0.5,60,200,45) field.font=native.newFont(native.systemFont,18) field:setTextColor(0.5) field.inputType="default" return field end local function mentionFriend(e) local previousText=textField.text textField:removeSelf() textField=nil textField=createField() textField.text=previousText.."@johndoe" native.setKeyboardFocus(textField) return true end textField=createField() local friend=widget.newButton{ width=200,height=45,left=60,top=100, font=native.systemFont,fontSize=14,label="@johndoe", labelColor={default={0},over={0}},onRelease=mentionFriend }

anyone have any idea? corona staff?

Hi @eja,

Which version of Corona are you using? Can you post your code?

Thanks,

Brent

Hi Brent, I am using the latest build (CoronaSDK 2014.2238). This is the most basic version I can write/think of:

local widget=require("widget") local textField local function addText(e) textField.text=textField.text.."testing" return true end textField=native.newTextField(display.contentWidth\*0.5,60,200,45) textField.font=native.newFont(native.systemFont,18) textField:setTextColor(0.5) textField.inputType="default" local addButton=widget.newButton{ width=200,height=45,left=60,top=100, defaultFile="button.png",overFile="button-over.png", font=native.systemFont,fontSize=14,label="Add Text to Field", labelColor={default={0},over={0}},onRelease=addText } timer.performWithDelay(1,function() native.setKeyboardFocus(textField) end)

If you try this code using the xcode simulator (ios device), you will notice after you type anything and then click the button, the cursor will always be placed at the end (adding to the field).

However , with Android, it will not, it will remain where you last typed.

Any techniques you can think of Brent?

No idea of your use case, but perhaps if you add/change the text inside the editing handler,  the cursor will set properly. However, it’s not very likely your app works this way. ie; if you ad/remove text based on user input inside the field, you could change the text during the handler call, and it should work.

Another strategy to work around the problem could be to destroy and recreate the field with the new text when you need to change the text on the fly, quickly.

Just a couple thoughts.

@mpappas sadly, the text thats being added is not based on user input (user is selecting friends list).

I did try your other strategy hoping for success and well… it didn’t work :frowning: What it actually did was place the cursor at the beginning (even tried using setKeyboardFocus after w/ a timer too, and still the cursor was at the beginning).

any more suggestions?

I am creating a text field with text in it (login field) in my app, and the cursor appears at the end of the text string… (android 4.3, latest SDK)…

It works for me in my app.

Can you post your new test code, or your actual app code that failed when setting the text first?


EDIT: Also, my field creation sets a field called “isEditable”, as below

[lua]

       editTextField = native.newTextField( tempX, tempY, tempW, tempH, keyboardHandlerEditText)
        editTextField.font = native.newFont( native.systemFont, tempSize )
        editTextField.inputType = keyboardType
        editTextField.align = ‘left’;
        editTextField.text = editText                   – Passed in text argument
        editTextField.isEditable = true               – allow user to edit
        editTextField:setTextColor( 0,0,0 )        – Native field apparently still uses setTextColor, not setFillColor after g2.0 release…
        editTextField.hasBackground = false     – true     – use true for debugging position of field
        editTextField.isSecure = isSecure

        native.setKeyboardFocus( editTextField )  – Get the keyboard coming onscreen…

        print("  editTextField created – textSize ==", tempSize)
        print(" x,y == ", editTextField.x, editTextField.y)

[/lua]

@mpappas you know, i think it was the fact I was using a timer around native.setKeyboardFocus (not even sure why i did). With a few modifications, this method for a fix you mentioned now is working, thanks bud. Posting for future reference:
 

Ultra stripped down working version for Android cursor position fix:

display.setDefault("background", 0.5) local widget=require("widget") local textField local function createField() local field=native.newTextField(display.contentWidth\*0.5,60,200,45) field.font=native.newFont(native.systemFont,18) field:setTextColor(0.5) field.inputType="default" return field end local function mentionFriend(e) local previousText=textField.text textField:removeSelf() textField=nil textField=createField() textField.text=previousText.."@johndoe" native.setKeyboardFocus(textField) return true end textField=createField() local friend=widget.newButton{ width=200,height=45,left=60,top=100, font=native.systemFont,fontSize=14,label="@johndoe", labelColor={default={0},over={0}},onRelease=mentionFriend }

I just saw this bug is still happening… This is a deal breaker for any business app and for my case specially is a huge problem that will make me lose a client contract.

Corona please take a look at it. This happens on the Corona Sample and I just submitted a bug case for it (Case 33076)

I just saw this bug is still happening… This is a deal breaker for any business app and for my case specially is a huge problem that will make me lose a client contract.

Corona please take a look at it. This happens on the Corona Sample and I just submitted a bug case for it (Case 33076)

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