string.len() always returns 0 for newTextField.text ?

No, because when I send code examples I always try to attach the minimal amount of code needed to reproduce the issue. This is mainly to make it easier for you. God knows I’ve been on your side of the table enough times to appreciate simple examples…  :slight_smile:

The mention of simulator etc was only to provide you with the reason why I needed to populate and read the contents of a native.newTextField in the first place.

But, sure, I can attach a more complete example:

local inSimulator = false if (system.getInfo("environment") == "simulator") then inSimulator = true end local editField = native.newTextField(display.contentWidth/2, 100, 200, 35) local function onPressMeTouch(e) if (e.phase == "began") then if (inSimulator) then print("In simulator - add dummy value to editField to be able to go on") editField.text = "123456" end -- Check if editField is populated if (string.len(editField.text) == 0) then native.showAlert("Error!", "Field must be populated!", {"Alright"}) end end end local pressMe = display.newText("PRESS ME!", display.contentWidth/2, 300, native.systemFont, 32) pressMe:addEventListener("touch", onPressMeTouch)

When the user presses the PRESS ME! label, a check is done to see if editField is populated and an alert is issued if it’s not.

To get past this in the simulator (where in Windows editField cannot be edited), I try to populate it with a dummy value before the population test.

That’s all there is to it. In the simulator I still get the alert. On an actual devide, the code works as expected.

I see now that the following is written in the docs/gothcas:

On Android, due to the fact that Corona runs on multiple threads, getting the .text value of a TextField immediately after setting it will not return the correct value. This action should be performed following a short timer or during the next time step.

If this is also the case for the windows simulator it explains the behaviour I’m experiencing.

Hi @runewinse,

Thanks for the (slightly more complete) code. Of course we appreciate the most minimalist code possible, but sometimes we need just a bit more.

The “multiple thread” thing might be the culprit, however, I was thinking a better Simulator workaround would be to not create a native text field at all (if the environment is the Simulator). Instead, just create a holding table with a “text” property, like this:

[lua]

local editField

if (inSimulator) then

   editField = { text="" }

else

   editField = native.newTextField(display.contentWidth/2, 100, 200, 35)

end

[/lua]

This way, you circumvent the entire text field thing in the Simulator and just use Lua to store its theoretical text, of which string.len() will clearly work.

By the way, have you considered using CoronaViewer for responsive device testing during development?

Brent

Hi Brent,

Thanks for the tip. Actually I ended up bypassing the entire newTextField and fed a dummy text directly into the target variable before switching scenes and found peace with the thread explanation…

I haven’t used CoronaViewer. I know it’s some kind of instant-on-the-device-view, but last time I read about it, you wrote that it only worked under iOS so I kind of forgot about it. 

Hi @runewinse,

Good to hear (text field thing). As for CoronaViewer, you may want to revisit this plugin:

https://github.com/coronalabs/CoronaViewer/blob/master/README.md

Brent