android: string format specify bold - android

I have a string defined in string.xml like
<string name="eventtitle">Title: %1$s </string>
which is formatted using string.format . How to define the string to get only the Titel: as Bold.
Thanks in advance for the help

You can do it like,
textView.setText(Html.fromHtml("<b>Title</b>: Text"));
If you have text in dynamic way..
And to define formatings in Strings.xml you can do like,
<string name="text1">This text uses <b>bold</b> and <i>italics</i>
by using inline tags such as <b> within the string file.</string>
see This Link

Now, it is reasonably supported by Android.
you can define the string in xml as <string name="text_to_show">Hey <b>This is in bold</b>
Then in code, use this to convert to CharSequence and then use it for e.g in TextView
String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);

Actually, many of the answers are obsolete. After researching by myself, what I've found out that the best answer is one by #Wahib. Here's the improved version:
Define the string resource as a:
<string name="styled_text">Hey, <b>this is bold</b> text</string>
Use the resource like this:
String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);
Here's the result:

You can use HTML markup like "<b>BOLD</b> other text ...".
See this Google resource for more information.

You have to style your string in the TextView in which in is displayed. See this link

Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf");
Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF");
String strt_title_desc=this.getResources().getString(R.string.eventtitle);
int upto=strt_title_desc.indexOf(":"); //of you can specify 5
if (strt_title_desc!=null)
{
aboutAuthTV.setTextColor(Color.BLACK);
aboutAuthTV.setLineSpacing(1.2f, 1.5f);
aboutAuthTV.setTextSize(23);
SpannableString SS = new SpannableString(strt_title_desc);
SS. setSpan ( new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SS. setSpan ( new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
yourtextView.setText(SS);
}
// this for changing the font size, style and colors
String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/>
<i>Then Italic</i> </font>" +
"<br/> <font color=\"green\" >this is simple sentence </font>" +
"<br/><br/><br/><br/><a>this is simple sentence</a>";
Spanned strHtml= Html.fromHtml(str);
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(strHtml);

Now, it is reasonably supported by Android.
I have already given answer to this how to add styling within a string

Related

how can I solve this error string xliff?

I have this string:
<string name="order_summary_name">Name: <xliff:g id="name" example="Thomas">%s</xliff:g></string>
using it in this format:
priceMessage=getString(R.string.order_summary_name,name);
and I keep getting an error that says: Format string 'order_summary_name' is not a valid string.
I cant find the mistake,what should I do ???
Store your styled text resource as an HTML-escaped string:
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
In this formatted string, a element is added. Notice that the opening bracket is HTML-escaped, using the < notation.
Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styled text:
String text = getString(R.string.welcome_messages, username, mailCount);
String styledText = Html.fromHtml(text);
(or)
String styledText = Html.fromHtml(R.string.order_summary_name,name);
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
The id attribute is just used to identify what the substitution parameter represents (in your case, it represents the quantity). It's as you said, a note, and not actually used programmatically.
That said, in Android Studio, if you have Code Folding enabled for strings, it will substitute in the ID when it shows the collapsed string. You'd see something like this:
Try this.
mTextView.setText(getString(R.string.order_summary_name, "Thoma"));

I need to change the color of only a file from my string

I have the following string:
<string name="welcome.location.services">Please <font color='green'>ALLOW</font> Passenger to access your location in order to track your trips.</string>
If I leave it like this, it will colour ALLOW with the default green. But I need a different colour there. How can I change it?
Tried:
<font color='#ff00ff'>ALLOW</font>
But the word will have no colour like this.
I want to do it in the strings.xml cause I have more files for each language, and the words length and start index is different, making spannable strings hard to work with
PS: I tried using HTML.fromHTML but still no luck:
((TextView)dialog.findViewById(R.id.header4)).setText(Html.fromHtml(context.getString(R.string.welcome_location_services)));
Where the string is:
<string name="welcome.location.services">Please <font color='#ffff00d3'>ALLOW</font> Passenger to access your location in order to track your trips.</string>
try this, and let me know.
String s = "Hello World, I'm the rest.";
String[] result = s.split(" ", 2);
String first = result[0];
String rest = result[1];
first = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + rest));
use CDATA which used with text data that should not be parsed by the XML parser.
use this:
<string name="welcome.location.services"><![CDATA[<b><font color=#FF0000>ALLOW!</b>]]></string>
For more info about CDATA check out this link.
From Ahmed's link, I assume this should help, <string name="title"> My <![CDATA[ <font color="#ae23ee">]] Cool <![CDATA[ </font> ]] Title</string>
So here only 'Cool' will be in that colour

Formatting Strings

People often link the following for explaining formatted strings, Can anyone show me a working example of how to use this?
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
well the name of the string is "welcome_message", for example you want to add that formated text into component, just edit layout.xml, find your component, and set the text on component to use that string
component < ....
android:text="#string/welcome_message
... />

Android TextView in BOLD and Normal text

I thoroughly searched on SO but didnt get answer of my question.
I want to set a paragraph, I will set it in XML using
The text contains Title and Steps and regular text. I want to make the Title and Steps in bold and rest in normal text.
I can do this by using different 's but how can I do it in the same TextView.
I mean using the same TextView how can I set different attributes for different sentences?
Use a Spannable String
TextView tv = (TextView) findViewById(R.id.tv);
String steps = "Hello Everyone";
String title="Bold Please!";
SpannableString ss1= new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(steps);
For more styling check the link # http://blog.stylingandroid.com/archives/177
in your strings file
<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content content content content content content content</p></p>
]]>
</string>
Then in your activity
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Html.fromHtml(getString(R.string.your_text)));
And output
You can format it like you would in HTML: let's call this custom_text
<b>Your title here</b>
This is the non-bolded stuff.
And then load the text using the Html class:
mTextView.setText(Html.fromHtml(getString(R.string.custom_text)));
That will create a spannable string and set it on the TextView.
please put this string in to res->string.xml
<string name="your_html">
<![CDATA[p><b>This is bold text</b> rementing is simple text
]]>
</string>
Now you can used whenever you have to require this thing.
tv.setText(Html.fromHtml(getString(R.string.your_html)));
It is bestway and work charm.
TextViews support SpannableStrings. You can either make your custom String or format your string in html and then set it with tv.setText(Html.fromHtml(yourString));
kotlin Solution :
For me, the issue was I had a text set in XML through data binding. I had to remove that. then this started taking effect
val spannableStringBuilder = SpannableStringBuilder(headerText)
spannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), 0, headerText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableStringBuilder.append(" ")
spannableStringBuilder.append(subText)
textView.text = spannableStringBuilder
The best way to do this is in the Strings resources.
<string name="sample_string"><![CDATA[<b>Title</b>StackOverflow]]></string>
Notice, the text between bold tag (<b> </b>) will appear in bold. Similarly, you can set other styles. For eg. <i> for italics and <u> for underline.
Hope this helps, good luck!
Instead, use this :
First, declare textView
TextView tv1 = (TextView) getActivity().findViewById(R.id.t1);
Then set it's text to the string you want
tv1.setText(Html.fromHtml(getString(R.string.my_text)));
Lastly, use set the typeface for your textView
tv1.setTypeface(null, Typeface.BOLD);
And, you are done.

How to highlight characters in String for Textview?

String = " This is SAMPLE"
I want to increase the size of the SAMPLE alone in String to highlight, but need to be in a single string, this is to set in TextView.
Thanks in Advance.
textView.setText(Html.fromHtml("This is <font color='#707070' size='20'>
SAMPLE</font>"));
Try this:
String str = "This is <b>SAMPLE</b>";
yourTextView.setText(Html.fromHtml(str), BufferType.SPANNABLE);
Moreover, you may decorate your string (str) with html tags to alter the looks.
Or to highlight a part of text without using html stuff, read this
Use Html.fromHtml() in setText() of TextView. Supported Html Tags can be found here Html Tags Supported by TextView
I think this is possible if you pass the text as html with your highlighted part enclosed in html <bold> tag.
You have two options:
1, Format your string in HTML and use HTML.from("Your text");
2, Use spannable. A complete guide can find link here

Categories

Resources