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

I’m trying to set the text of a newTextField and then check the lenght of the string in the field, but string.len() always returns 0.

Consider the following snippet:

local field1 = native.newTextField( 50, 100, 100, 35 ) field1.text = "" print("field1.text length = " .. string.len(field1.text)) field1.text = "123456" print("field1.text length = " .. string.len(field1.text))

This snippet writes this in the console:

field1.text length = 0

field1.text length = 0

The first one does (and should) write length = 0, but why does the second one do it?

I’ve probably done some stupid error that is too simple to detect, but what?

Hi @runewinse,

Handling and checking native fields/content is typically done on an “event-based” methodology. Please see the examples in the docs here:

http://docs.coronalabs.com/api/library/native/newTextField.html

Thanks,

Brent

Hi,

Do you mean I should (for each newTextField):

  1. Set up a listener

  2. Inside the editing phase in the listener, copy event.target.text to some scene level variable

  3. Check this variable when the time comes (typically at OK/APPLY in a business app).

I’m not sure I understad what you mean by typically". Is it not a “strictly legal” way to fetch the contents of a newTextField the way I’ve done? Or is it working, but in an unreliable way?

The reason I ask is that this is how I’ve always checked if te user has written anything in the text field. I’ve also always  just used textField.text to get the string data directly for further processing, and it works very well in many other parts of the app.

Hi @runewinse,

I’m not sure why this is occurring. I copied your exact code into a test app (OS X Simulator) and the first print() yields 0 while the second yields 6, just as it should.

Brent

Well, I’m on Windows. I don’t know if that has anything to do with it…

Here’s the exact output from the simulator:

Copyright © 2009-2014  C o r o n a   L a b s   I n c .

        Version: 3.0.0

        Build: 2014.2511

Platform: Sensation / x64 / 6.2 / GeForce GTX 750 Ti/PCIe/SSE2 / 4.4.0 NVIDIA 344.60 / 2014.2511

field1.text length = 0

field1.text length = 0

It would have been nice if you tried the exact code with the exact same simulator to see what happens.

I’ve attached the main.lua file.

(I had to zip it, because (for some strange reason) .lua files are not allowed to be attached.)

Hi @runewinse,

How are you using native.newTextField in the Windows Simulator? It’s not available in that environment. Are you testing on some device?

Thanks,

Brent

Hi,

I’m not testing native.newTextField in the windows simulator by entering or displaying text (which I know all to well is not possible).

I only create it, and populate/read the .text parameter. You can see all and everything I do in main.lua attached.


To go into details:

I have a business kind of app with some native.newTextFields. In one of the scenes the user must fill in some value to get to the next screen/scene. With the (very annoying) lack of native.newTextField support in the Windows simulator I detected when running in the simulator and if so tried to populate the native.newTextField with some dummy value just to be able to get further.

So this is how this whole ting started…

Hi @runewinse,

I checked you zipped attachment from a couple posts up, and there’s no checking of the enviornment (Simulator or not) so I’m not sure how you’re handling that in your code. Can you post more complete example code? I don’t necessarily need a zipped file… you can just post the code in your response.

Brent

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

Hi @runewinse,

Handling and checking native fields/content is typically done on an “event-based” methodology. Please see the examples in the docs here:

http://docs.coronalabs.com/api/library/native/newTextField.html

Thanks,

Brent

Hi,

Do you mean I should (for each newTextField):

  1. Set up a listener

  2. Inside the editing phase in the listener, copy event.target.text to some scene level variable

  3. Check this variable when the time comes (typically at OK/APPLY in a business app).

I’m not sure I understad what you mean by typically". Is it not a “strictly legal” way to fetch the contents of a newTextField the way I’ve done? Or is it working, but in an unreliable way?

The reason I ask is that this is how I’ve always checked if te user has written anything in the text field. I’ve also always  just used textField.text to get the string data directly for further processing, and it works very well in many other parts of the app.

Hi @runewinse,

I’m not sure why this is occurring. I copied your exact code into a test app (OS X Simulator) and the first print() yields 0 while the second yields 6, just as it should.

Brent

Well, I’m on Windows. I don’t know if that has anything to do with it…

Here’s the exact output from the simulator:

Copyright © 2009-2014  C o r o n a   L a b s   I n c .

        Version: 3.0.0

        Build: 2014.2511

Platform: Sensation / x64 / 6.2 / GeForce GTX 750 Ti/PCIe/SSE2 / 4.4.0 NVIDIA 344.60 / 2014.2511

field1.text length = 0

field1.text length = 0

It would have been nice if you tried the exact code with the exact same simulator to see what happens.

I’ve attached the main.lua file.

(I had to zip it, because (for some strange reason) .lua files are not allowed to be attached.)

Hi @runewinse,

How are you using native.newTextField in the Windows Simulator? It’s not available in that environment. Are you testing on some device?

Thanks,

Brent

Hi,

I’m not testing native.newTextField in the windows simulator by entering or displaying text (which I know all to well is not possible).

I only create it, and populate/read the .text parameter. You can see all and everything I do in main.lua attached.


To go into details:

I have a business kind of app with some native.newTextFields. In one of the scenes the user must fill in some value to get to the next screen/scene. With the (very annoying) lack of native.newTextField support in the Windows simulator I detected when running in the simulator and if so tried to populate the native.newTextField with some dummy value just to be able to get further.

So this is how this whole ting started…

Hi @runewinse,

I checked you zipped attachment from a couple posts up, and there’s no checking of the enviornment (Simulator or not) so I’m not sure how you’re handling that in your code. Can you post more complete example code? I don’t necessarily need a zipped file… you can just post the code in your response.

Brent