newline character (\n) in display.newText

Hello,

Is it possible to use \n (newline character) with display.newText ? When I use it on the simulator, it works. However, it doesn’t work when I run the apk on an android phone.

Thanks,
Cem [import]uid: 92080 topic_id: 15794 reply_id: 315794[/import]

\n isn’t currently usable however you can use this; http://developer.anscamobile.com/code/auto-wrapped-text-updated

Peach :slight_smile: [import]uid: 52491 topic_id: 15794 reply_id: 58343[/import]

Thanks, an interesting but nice way to approach the problem :slight_smile: [import]uid: 92080 topic_id: 15794 reply_id: 58354[/import]

you can also do something like this

[blockcode]
txt ={ “line1”},{“line2”},{“line3”}
for a=1,#txt do
distxt=display.newText( txt[a], 100, 50+(a*30), native.systemFont,16)
end [import]uid: 7911 topic_id: 15794 reply_id: 58359[/import]

Seriously and ideally, why use some much lines of codes when a simple ‘\n’ should had done the same job?

It would be great Ansca correct the problem.

Cheers. [import]uid: 87460 topic_id: 15794 reply_id: 58507[/import]

Is \n newline still not working on Android? Works on simulator, works on iOS devices, but no go on the two android devices I tested on.

EDIT: I made a test app and it does appear to be working on the Nook. So I need to figure out what I’m doing different in my regular app. Something strange going on…

EDIT ii: Definitely something not working right on Android devices when entering text into a native textbox and pressing return (triggering a newline). I’m losing all the text typed after the newline when I transfer the entered text to a display.newText object. I entered a bug, and more detailed info can be found here:
http://developer.coronalabs.com/forum/2012/10/25/strange-behavior-when-pressing-return-editable-native-textbox-android-devices
[import]uid: 9422 topic_id: 15794 reply_id: 128561[/import]

Hmm… I’ve never seen this issue. I heavily tested multiline text on Android with my (office famous) Gettysburg Address test and it worked.

[lua]local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(128, 0, 0)

local gettysburgAddress = [[Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people, shall not perish from the earth.]]

local message = gettysburgAddress
local textWidth = display.contentWidth - 50
local textHeight = display.contentHeight - 50
local fontSize = 18

local textField = display.newText(message, 25, 25, textWidth, textHeight, “Helvetica”, fontSize )
textField:setTextColor( 255, 255, 255 )[/lua]

I’ve also tried enter explicit new line characters within string via ‘\n’ as well and it has worked.

So, I’m curious as to what are the differences are between your code and what I have shown above. I know the above code works. And please give the above code a go on your Android devices if you don’t believe me. We just need to isolate what exactly is going wrong. [import]uid: 32256 topic_id: 15794 reply_id: 128676[/import]

Is \n newline still not working on Android? Works on simulator, works on iOS devices, but no go on the two android devices I tested on.

EDIT: I made a test app and it does appear to be working on the Nook. So I need to figure out what I’m doing different in my regular app. Something strange going on…

EDIT ii: Definitely something not working right on Android devices when entering text into a native textbox and pressing return (triggering a newline). I’m losing all the text typed after the newline when I transfer the entered text to a display.newText object. I entered a bug, and more detailed info can be found here:
http://developer.coronalabs.com/forum/2012/10/25/strange-behavior-when-pressing-return-editable-native-textbox-android-devices
[import]uid: 9422 topic_id: 15794 reply_id: 128561[/import]

Hi Joshua,

This might be a corner case as some of my own tests worked as expected, and I don’t doubt your Gettysburg code works on Android devices.

But if you take a look at the code with the bug I submitted (18319) it illustrates the problem. I took the existing coronalabs sample code NativeDisplayObjects and modified it so the native textbox input is also printed as a newText display object below the native box. On the simulator, if while you are entering text in the textbox you press the enter key on the keyboard, a newline is generated as expected. It works this way on iOS devices as well. But if you run the same code on an Android device the newline doesn’t register, and any further text entered is lost (ie it never makes it to the newText display object). In my own app the entire block of text including everything entered before the first newline is lost.

[import]uid: 9422 topic_id: 15794 reply_id: 128701[/import]

Hmm… I’ve never seen this issue. I heavily tested multiline text on Android with my (office famous) Gettysburg Address test and it worked.

[lua]local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(128, 0, 0)

local gettysburgAddress = [[Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people, shall not perish from the earth.]]

local message = gettysburgAddress
local textWidth = display.contentWidth - 50
local textHeight = display.contentHeight - 50
local fontSize = 18

local textField = display.newText(message, 25, 25, textWidth, textHeight, “Helvetica”, fontSize )
textField:setTextColor( 255, 255, 255 )[/lua]

I’ve also tried enter explicit new line characters within string via ‘\n’ as well and it has worked.

So, I’m curious as to what are the differences are between your code and what I have shown above. I know the above code works. And please give the above code a go on your Android devices if you don’t believe me. We just need to isolate what exactly is going wrong. [import]uid: 32256 topic_id: 15794 reply_id: 128676[/import]

Stephen,
Okay, I’ll have a look at the bug you’ve submitted. It sounds like the issue is that the string you are getting back from the TextBox is missing \n characters… because I know our display.newText() function supports \n characters.

Everyone else,
Can you post a short Lua script that reproduces this issue please? Or are you running into the same native TextBox issue that Stephen is running into? Because \n characters passed to display.newText() is definitely working for me. There’s gotta be something else to it and I would love it if you guys can help me track it down. Thanks! [import]uid: 32256 topic_id: 15794 reply_id: 128797[/import]

Hi Joshua,

This might be a corner case as some of my own tests worked as expected, and I don’t doubt your Gettysburg code works on Android devices.

But if you take a look at the code with the bug I submitted (18319) it illustrates the problem. I took the existing coronalabs sample code NativeDisplayObjects and modified it so the native textbox input is also printed as a newText display object below the native box. On the simulator, if while you are entering text in the textbox you press the enter key on the keyboard, a newline is generated as expected. It works this way on iOS devices as well. But if you run the same code on an Android device the newline doesn’t register, and any further text entered is lost (ie it never makes it to the newText display object). In my own app the entire block of text including everything entered before the first newline is lost.

[import]uid: 9422 topic_id: 15794 reply_id: 128701[/import]

Here’s a small bit of code to illustrate the problem.

If you run this on the simulator (mac) you will note that what you type in the textbox is repeated below it in a display.newText object, as you type. Each keypress will be printed in the console as well. If you press the ‘return’ key this, too, will register as a keypress. Type a bunch of lines and 'return’s to see the keypresses continue to be registered. If you build and run this on an iOS device you will see similar results.

But if you run it on an Android device you will see that as soon as you press ‘return’, nothing you press afterwards will register in the display.newText object. If you check logcat to view the console output you will see none of the keypresses register after that first ‘return’. The native textbox does continue to accept keypresses and returns but it is no longer captured by the display object.

-----------------------------------------------------------------------------------------  
--TEST NEWLINE bug in native textbox on Android  
-- New lines from a textbox work on Corona OSX simulator, and iOS devices.   
--But on Android devices pressing the return key never registers as a key event.  
-----------------------------------------------------------------------------------------  
  
local textBox = native.newTextBox( 20, 20, 280, 100 )  
  
local displayText = display.newText( "Native TextBox text will be repeated here. Pressing return will register a new line on simulator and iOS devices, but NOT on Android devices.", 20, 120, 280, 300, native.systemFont, 20)  
displayText:setTextColor(255, 255, 255)  
  
function inputListener( event )  
 if event.phase == "began" then  
 elseif event.phase == "ended" then   
 displayText.text = textBox.text   
 elseif event.phase == "editing" then  
 displayText.text = textBox.text --updates the text one character at a time.   
 print("added keystroke: "..event.newCharacters)--print keystroke to console to prove it's not registering on Android devices. Use logcat to see.  
 end  
end  
  
textBox.size = 20  
textBox:setTextColor( 0, 0, 255, 255 )  
textBox.isEditable = true  
textBox:addEventListener( "userInput", inputListener)   
  
native.setKeyboardFocus( textBox ) --pops up the keyboard so editing can begin immediately  

Incidentally, how can I append this new code to the existing bug I entered? [import]uid: 9422 topic_id: 15794 reply_id: 128838[/import]

Stephen,
Okay, I’ll have a look at the bug you’ve submitted. It sounds like the issue is that the string you are getting back from the TextBox is missing \n characters… because I know our display.newText() function supports \n characters.

Everyone else,
Can you post a short Lua script that reproduces this issue please? Or are you running into the same native TextBox issue that Stephen is running into? Because \n characters passed to display.newText() is definitely working for me. There’s gotta be something else to it and I would love it if you guys can help me track it down. Thanks! [import]uid: 32256 topic_id: 15794 reply_id: 128797[/import]

Here’s a small bit of code to illustrate the problem.

If you run this on the simulator (mac) you will note that what you type in the textbox is repeated below it in a display.newText object, as you type. Each keypress will be printed in the console as well. If you press the ‘return’ key this, too, will register as a keypress. Type a bunch of lines and 'return’s to see the keypresses continue to be registered. If you build and run this on an iOS device you will see similar results.

But if you run it on an Android device you will see that as soon as you press ‘return’, nothing you press afterwards will register in the display.newText object. If you check logcat to view the console output you will see none of the keypresses register after that first ‘return’. The native textbox does continue to accept keypresses and returns but it is no longer captured by the display object.

-----------------------------------------------------------------------------------------  
--TEST NEWLINE bug in native textbox on Android  
-- New lines from a textbox work on Corona OSX simulator, and iOS devices.   
--But on Android devices pressing the return key never registers as a key event.  
-----------------------------------------------------------------------------------------  
  
local textBox = native.newTextBox( 20, 20, 280, 100 )  
  
local displayText = display.newText( "Native TextBox text will be repeated here. Pressing return will register a new line on simulator and iOS devices, but NOT on Android devices.", 20, 120, 280, 300, native.systemFont, 20)  
displayText:setTextColor(255, 255, 255)  
  
function inputListener( event )  
 if event.phase == "began" then  
 elseif event.phase == "ended" then   
 displayText.text = textBox.text   
 elseif event.phase == "editing" then  
 displayText.text = textBox.text --updates the text one character at a time.   
 print("added keystroke: "..event.newCharacters)--print keystroke to console to prove it's not registering on Android devices. Use logcat to see.  
 end  
end  
  
textBox.size = 20  
textBox:setTextColor( 0, 0, 255, 255 )  
textBox.isEditable = true  
textBox:addEventListener( "userInput", inputListener)   
  
native.setKeyboardFocus( textBox ) --pops up the keyboard so editing can begin immediately  

Incidentally, how can I append this new code to the existing bug I entered? [import]uid: 9422 topic_id: 15794 reply_id: 128838[/import]

Thanks for this Stephen. I’ll have a quick look at this later this week. [import]uid: 32256 topic_id: 15794 reply_id: 129249[/import]

Thanks Joshua. My app relies on the ability to enter multi-paragraph text into a native textbox and then do something useful with it, and I haven’t found a workaround, so this bug is a showstopper for me on Android.

[import]uid: 9422 topic_id: 15794 reply_id: 129255[/import]

Thanks for this Stephen. I’ll have a quick look at this later this week. [import]uid: 32256 topic_id: 15794 reply_id: 129249[/import]

Thanks Joshua. My app relies on the ability to enter multi-paragraph text into a native textbox and then do something useful with it, and I haven’t found a workaround, so this bug is a showstopper for me on Android.

[import]uid: 9422 topic_id: 15794 reply_id: 129255[/import]

Stephen,

I just tried the code you posted up above with the newest daily build #942 on my Galaxy Nexus (Android 4.1 Jellybean) and it’s working. The text display object is showing a line break when I press the Enter key and your “added keystroke:” print statement is going off when I press the Enter key.

So, it sounds like this issue might by OS version or device specific. Can you tell me what make/model of your device is and the Android OS version it is running please? Thanks. [import]uid: 32256 topic_id: 15794 reply_id: 129396[/import]