How to create multiple lines of text in android? - android

In my project I need to display the instructions page. The page consists of 8 instructions. So do we need to create 8 textviews to display those instructions? Is it possible to display all those instructions using one single textview?
I read about multi-line textviews.
But for my case can I do so to display all 8 instructions in one textview?

As far as I recall, the newline character \n is recognised when used in strings.xml, so you could declare your instructions there and reference it in the layout. I never found a way to do it within the layout XML itself, but there may be something.
Edit: for clarity - I mean using the escaped character, e.g.:
<string name="instructions">Line 1\nLine 2</string>

You can use \n new line character from string resource or thru code:textView.setText("1st line\n 2nd line");

You can use html to format your string: textView.setText(Html.fromHtml("item 1<br>item 2<br>"));

You can use: StringBuilder to it.

Related

How to gives space to paragraph inside string value

I have some text inside string of value folder in android studio and I want to give space between paragraph but I don't know how to do that ?
you can use \n for new line and add space by entering space
for example:
<string name="above_eightteen">I confirm that \n I am above 18 years and agree to all Age restriction disclaimer.</string>
and use this string value in your layout.
Most probably your text will be a straight line in the string.xml file. find out the point where a paragraph is starting or ending, and put "\n" before paragraph start or after paragraph end.

Styling Text inside Android TextView

In my app, each Target Text has a mix of numbers and strings. I want the numbers to be highlighted in Bold and string to be italicised. For e.g.
You have reached 25% of your goal. 30 more days to achieve target
Is there an easier way to do it than create 5 separate textviews for this - 3 for string and 2 for number. Managing layout of 5 textboxes for each target is becoming a nightmare in my layout. I have 9 such rows leading to potentially 45 textviews in one layout.
You can use <i> and <b> html tags within your string, like this:
<string name="my_string"><i>You have reached</i> <b>25%</b> <i>of your goal.</i> <b>30</b> <i>more days to achieve target</i></string>
You can read more here, under the Styling with HTML markup section: https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
You can use the following example,
For Kotlin
textView.text = Html.fromHtml("<b>Hello</b> <i>World</i>")
For Java
textView.setText(Html.fromHtml("<b>Hello</b> <i>World</i>"))

How to make a link INSIDE a string resource (along with other text in it) ? - Android

For example: Take a look at the following string resource:
<string name="b1b">This link will take you to google.com. More text here.</string>
Now I want this string resource to look like this in my app:
This link will take you to Google. More text here.
I can't use three textviews. This was just an example. So I can't make the entire textview a link.
(Why?
What I'm doing in my app is … I have say a dozen buttons, each of them sends a string resource ID as an intent to a "Text Shower Activity" … and in that I simply have a single textview which shows different texts based on which button the user clicked. So, I'm saving on app size.
Plus every such text string resource has different number of links at different places, so it's not feasible to have a single textview just for links and somehow weave it in between.)
So, I need to make a little bit of the string resource into a link. I've tried the <a> thing with no effect.
How to do this?
Example XML resource:
</string name="mlink">
to go to Google<![CDATA[ click here]]>
and <![CDATA[this]]> moves you to yahoo!
</string>
Java:
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));
Note that you need to put your html link inside the CDATA tag, this is the proper way to use links in String resources.
Just to add on #Droidman's answer if anyone wants to use it without CDATA, Below will also work without CDATA but we have to escaped the characters such as "<" , using the < notation.
</string name="mlink">
to go to Google <a href=\"http://google.com\">click here</a>
and <a href=\"http://yahoo.com\">this</a> moves you to yahoo!
</string>
Java:
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));

String Resource new line /n not possible?

It doesn't seem like it's possible to add a new line /n to an XML resource string. Is there another way of doing this?
use a blackslash not a forwardslash. \n
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello\nWorld!</string>
</resources>
Also, if you plan on using the string as HTML, you can use <br /> for a line break(<br />)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello<br />World!</string>
</resources>
I know this is pretty old question but it topped the list when I searched. So I wanted to update with another method.
In the strings.xml file you can do the \n or you can simply press enter:
<string name="Your string name" > This is your string.
This is the second line of your string.\n\n Third line of your string.</string>
This will result in the following on your TextView:
This is your string.
This is the second line of your string.
Third line of your string.
This is because there were two returns between the beginning declaration of the string and the new line. I also added the \n to it for clarity, as either can be used. I like to use the carriage returns in the xml to be able to see a list or whatever multiline string I have. My two cents.
After I tried next solution
add \n
add \n without spaces after it
use "" and press Enter inside text
text with press Enter
use lines=2
What solves me is br tag
<string name="successfullyfeatured">successfully<br/> You are now a member</string>
Update
the most reliable solution is to use Translation editor and edit text and it will handle new lines for you
This is an old question, but I found that when you create a string like this:
<string name="newline_test">My
New line test</string>
The output in your app will be like this (no newline)
My New line test
When you put the string in quotation marks
<string name="newline_test">"My
New line test"</string>
the newline will appear:
My
New line test
When using the translations editor in Android Studio just click the icon to the right (or use Shift-Enter on Windows, Alt/Option-Enter on MacOS) and then add line breaks using return.
This will insert \n correctly in the localized strings.xml.
In the latest version of Android studio, "\n" is going to be printed like it was meant to be there unless the whole string it's in apostrophes
For Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">"Hello\nWorld!"</string>
</resources>
I have tried two options, both are efficient and easy.
I used <br/> HTML tag
<string name="name_detail">
Hey user. <br/>
Enjoy a new shopping deal.!!
</string>
Add an encoding line at the top of strings.xml
<?xml version="1.0" encoding="utf-8"?>
and use "\n" to break line
Example
<string name="name_detail">
Hey user.\n
Enjoy a new shopping deal.!!
</string>
If you put "\n" in a string in the xml file, it's taken as "\\n"
So , I did :
text = text.Replace("\\\n", "\n"); ( text is taken from resX file)
And then I get a line jump on the screen
don't put space after \n, otherwise, it won't work. I don't know the reason, but this trick worked for me pretty well.
You can use this format
<string name="test">
"This is
a new line"
</string>
I just faced this issue.
didn't work on TextView with constraint parameters. Adding android:lines="2" seems to fix this.
I have tried
1-Top word \n Bottom word
2-Top word ENTER Bottom word
Didn't work
And finally <br/> worked for me in android studio
In fact the correct way to use it is \n although it is possible to see it done after Build the application. The only has an effect on the design and not on the application
<string name="available_balance">Available\nBalance</string> //Look good on the app and Available\nBalance in android studio
<string name="available_balance">Available<br/>Balance</string> //Look good in android studio and Available Balance on the App
finally, I found the solution.
the <br/> is working on design only and it's not working in running app. the \n is working on running app only and not working on design
so the solution is to use both if you want to have both results.
something like this
\n<br/>
Example:
Hello \n<br/>World.
Result in design mode:
Hello\n
World.
Result in running app:
Hello
World.
Very simple you have to just put
\n
where ever you want to break line in your string resource.
For example
String s = my string resource have \n line break here;
I want to expand on this answer.
What they meant is this icon:
It opens a "real editor window" instead of the limited-feature text box in the big overview. In that editor window, special chars, linebreaks etc. are allowed and converted to the correct xml "code" when saved
Just use "\n" in your strings.xml file as below
<string name="relaxing_sounds">RELAXING\nSOUNDS</string>
Even if it doesn't looks 2 lines on layout actually it is 2 lines. Firstly you can check it on Translation Editor
Click the down button and you will see this image
Moreover if you run the app you will see that it is written in two lines.
\n tag worked in the strings.xml file
Eg:
<string name="hello">Hello World &\nWelcome</string>
Output:
Hello World &
Welcome
Note:
Make sure that you dont add space before and after of \n"
Even if it doesnt looks on new line in the preview window of UI when you run the app it will be displayed in the next line.
To see in the design tab the line breaks and in the device i use:
<br/><br/>\n\n
(to 2 break lines)
Although the actual problem was with the forward slash still for those using backslash but still saw \n in their layout.
Well i was also puzzled at first by why \n is not working in the layout but it seems that when you actually see that layout in an actual device it creates a line break so no need to worry what it shows on layout display screen your line breaks would be working perfectly fine on devices.
Late answer but i think might help someone.
You will some times see \n in the textview in android studio's layout preview. (But it lies then). Please refresh layout.
Or deploy your app to a real Android Device to see the \n actually working in android string resource.
Have you tried turning it off and on again ?!
This is a very old question but no other answer seem to suggest this.
It could be that the layout preview on android studio is not properly displaying the textView.
So you could try exiting android studio and launching it again.
I guess this can happen if android studio was open while the computer was in sleep/hibernate etc.
Anyway, worth a shot.

Line break in Android adds padding

I have 5-10 lines of address information that I want to insert in a layout in my Android app.
I rather not use seperate textviews, but want to have one where I can insert line breaks manually.
I do this by adding \n and it seems to work, BUT.. This also adds padding or a space, I'm not sure which one.
Example:
This XML
<string name="contact_address">
Street address\n
City\n
Country
</string>
Gives this output:
Street address
City
Country
Anyone know what could be wrong, or do I have to give up and make 5-10 seperate textviews?
Try:
<string name="contact_address">Street address\nCity\nCountry</string>
to get rid of the spaces you are putting into your XML.
I stumbled upon this and figured out a better way to keep the XML formatting and the line breaks without additional padding.
Use
<string>Line 1
\nLine 2
\nLine 3
</string>
Instead of
<string>Line 1\n
Line 2\n
Line 3
</string>

Categories

Resources