4

I am trying to split the single string in to two lines using \n in android but it shows as it is with \n too. I am expecting below,

String new = "Hi, How are you";

In EditText box I need,

Hi,

How are you

I know its simple question but I couldn't find a solution.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
vinothp
  • 9,939
  • 19
  • 61
  • 103

4 Answers4

10

I think you are new into android development like me. It's really easy to do...

You just have to do this:

editText.setText("Hi,\nHow are you?");
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
MAC
  • 15,799
  • 8
  • 54
  • 95
  • thanks for your reply... Actually \n is not working if i pass string to edtitext, but it works inside edittext.settext(); thaks – vinothp Apr 02 '12 at 22:59
  • I think this is a great question. I embedded '\n' in a String (Java SE) and was confused as to why EditText did not place a new line. It only happens in setText. Hmmm been programming for 18 years and "people do like this" if it saves them time --- quirky behavior that waste my time - like this - I likeeeee. – Matthew Ferguson May 05 '16 at 21:02
9

It's "\n" - a backslash, not a forward slash.

See this related question for more information about how to make a multiline text view look right.

Community
  • 1
  • 1
ravuya
  • 8,586
  • 4
  • 30
  • 33
  • Thanks for your answer but the thing is if i insert a \n in this sting String new = "Hi,\n How are you"; it should come in new line is it.. or i am trying in wrong syntax – vinothp Mar 31 '12 at 23:29
4

In your case, replace "/n" and with "\n" and also if you need more then one EditText, then you must edit properties of EditText as below:

android:singleLine="false" 
android:MaxLines = "5"

if you know the exact number of lines.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Ishu
  • 5,357
  • 4
  • 16
  • 17
0

yes "\n" is supported in android, unfortunately "/n" is not supported yet ;)

Mayank
  • 8,777
  • 4
  • 35
  • 60