How to make XML strings bold, underlined etc?
Asked Answered
L

5

15

http://docs.fusioncharts.com/charts/contents/Styles/Font.html

I tried this, along with a lot of things but failed to do so. Here's what I want.

<string name="ss">Bold. Underlined. Italic. Big. Small</string>

I want to format a little bit of the string. Where it's written bold, I want it to be bold...and same for others.

I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.

I know how to do it in a textview, but that's not what I want...

I'm sending some text resource to an activity that shows it... If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.

Is there a way to simple do this in the XML file ? or some other way ?

Latinity answered 22/7, 2013 at 8:37 Comment(1)
A part of the string should have a property like bold or underlined or bigger size... only a part of the string not the entire string.Latinity
P
22

Try wrapping your marked up text in CDATA tags. For example:

<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>

And then use Html.fromHtml wherever you're wanting to display it:

Html.fromHtml(getString(R.string.ss))
Pompeii answered 26/1, 2014 at 0:23 Comment(0)
P
6

This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!

Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/

The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags. My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.

Prochoras answered 8/9, 2014 at 21:9 Comment(1)
Keep in mind that if you use getText you lose the ability to use placeholders such as %s and %dFreeliving
C
5

exemple:

<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>

b = bold et u = underline .....etc

Chaffer answered 23/7, 2013 at 13:28 Comment(2)
This does work in your res/values/strings.XML, I've done it a thousand times. It is different than HTML tags.Affect
You have to use getText(string id) instead of getStringEscargot
W
3

This is working for me.

<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>

txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));

more details check Official site: https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables

Wethington answered 8/4, 2018 at 6:36 Comment(0)
C
-7

in dimens file write:

<dimen name="size_edittext">180dp</dimen>

and in your xml layout or activity call it:

android:@dimen/ size_edittext
Chaffer answered 24/7, 2013 at 14:37 Comment(1)
This is not useful answer. Delete this post.Point

© 2022 - 2024 — McMap. All rights reserved.