How to construct android AttributedString for api level 11 - android

I know how to do this in iOS but not yet in android. How do I construct an attributed string in android where a portion is bold as in
"this is the example with a bold portion"

FWIW, I have never seen AttributedString used, in ~6.5 years of Android development work.
A class implementing Spanned contains markup rules ("spans"). The simplest way to construct one dynamically is by using Html.fromHtml() to parse an HTML string with basic tags like <b>. String resources (e.g., res/values/strings.xml) also support <b>, <i>, and <u> tags.
Or, you can apply spans yourself. In the sample code that follows, I get the CharSequence from a TextView, remove all existing spans, and highlight search terms with a BackgroundColorSpan:
private void searchFor(String text) {
TextView prose=(TextView)findViewById(R.id.prose);
Spannable raw=new SpannableString(prose.getText());
BackgroundColorSpan[] spans=raw.getSpans(0,
raw.length(),
BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}
int index=TextUtils.indexOf(raw, text);
while (index >= 0) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
prose.setText(raw);
}
(from this sample project)
For boldface or italics, you would use StyleSpan instead of BackgroundColorSpan, and so forth.

Related

Markdown support in Android TextView

Is there a way to enable a TextView to detect markdown tags and render the text accordingly? More specifically, my app contains a TextView in which the users can provide a description, and often they will use markdown to format their description. Unfortunately the text doesn't render, and instead we see all the tags written out in the textview.
There's no built-in support for Markdown in the Android SDK. You'll have to use a lib like markdown4j or CommonMark.
I understand you want to convert a String containing Markdown markup to a formatted CharSequence that you can use in a TextView. The two options I know of are :
Bypass : Use a native C library to parse the text. Unfortunately the project seems dead.
commonmark-spannable-android : Pure java, based on the very good commonmark-java
library.
I used both and in my opinion, the second one is better : no need to deal with native architectures, smaller APK, and the performance is quite good (something like 2 times slower in my case, with is more than good enough)
Update : Found another option (it's the one I'm using now) :
Markwon : Pure java, also using commonmark-java as parser, with optional support for images and tables
There is no inherit support for markdown in textview, however if you only need simple markdown-lite implementation via simple "regexp" matching, this section from my "load readme from project root folder" in https://github.com/mofosyne/instantReadmeApp would help.
Note that this does not remove the markup in the text, only styles the lines differently. This may be a good or bad thing, depending on your application.
Oh and the nice thing? It styles in native textview, so the text is still selectable like normal text.
Specifically this line: https://github.com/mofosyne/instantReadmeApp/blob/master/app/src/main/java/com/github/mofosyne/instantreadme/ReadMe.java#L137
Slightly modified below: private void updateMainDisplay(String text) to private void style_psudomarkdown_TextView(String text, TextView textview_input), so you could use the same function for different textviews
```
/*
Text Styler
A crappy psudo markdown styler. Could do with a total revamp.
*/
/*
* Styling the textview for easier readability
* */
private void style_psudomarkdown_TextView(String text, TextView textview_input) {
//TextView mTextView = (TextView) findViewById(R.id.readme_info);
TextView mTextView = textview_input;
// Let's update the main display
// Needs to set as spannable otherwise http://stackoverflow.com/questions/16340681/fatal-exception-string-cant-be-cast-to-spannable
mTextView.setText(text, TextView.BufferType.SPANNABLE);
// Let's prettify it!
changeLineinView_TITLESTYLE(mTextView, "# ", 0xfff4585d, 2f); // Primary Header
changeLineinView(mTextView, "\n# ", 0xFFF4A158, 1.5f); // Secondary Header
changeLineinView(mTextView, "\n## ", 0xFFF4A158, 1.2f); // Secondary Header
changeLineinView(mTextView, "\n---", 0xFFF4A158, 1.2f); // Horizontal Rule
changeLineinView(mTextView, "\n>", 0xFF89e24d, 0.9f); // Block Quotes
changeLineinView(mTextView, "\n - ", 0xFFA74DE3, 1f); // Classic Markdown List
changeLineinView(mTextView, "\n- ", 0xFFA74DE3, 1f); // NonStandard List
//spanSetterInView(String startTarget, String endTarget, int typefaceStyle, String fontFamily,TextView tv, int colour, float size)
// Limitation of spanSetterInView. Well its not a regular expression... so can't exactly have * list, and *bold* at the same time.
spanSetterInView(mTextView, "\n```\n", "\n```\n", Typeface.BOLD, "monospace", 0xFF45c152, 0.8f, false); // fenced code Blocks ( endAtLineBreak=false since this is a multiline block operator)
spanSetterInView(mTextView, " **" , "** ", Typeface.BOLD, "", 0xFF89e24d, 1f, true); // Bolding
spanSetterInView(mTextView, " *" , "* ", Typeface.ITALIC, "", 0xFF4dd8e2, 1f, true); // Italic
spanSetterInView(mTextView, " ***" , "*** ", Typeface.BOLD_ITALIC, "", 0xFF4de25c, 1f, true); // Bold and Italic
spanSetterInView(mTextView, " `" , "` ", Typeface.BOLD, "monospace", 0xFF45c152, 0.8f, true); // inline code
spanSetterInView(mTextView, "\n " , "\n", Typeface.BOLD, "monospace", 0xFF45c152, 0.7f, true); // classic indented code
}
private void changeLineinView(TextView tv, String target, int colour, float size) {
String vString = (String) tv.getText().toString();
int startSpan = 0, endSpan = 0;
//Spannable spanRange = new SpannableString(vString);
Spannable spanRange = (Spannable) tv.getText();
while (true) {
startSpan = vString.indexOf(target, endSpan-1); // (!##$%) I want to check a character behind in case it is a newline
endSpan = vString.indexOf("\n", startSpan+1); // But at the same time, I do not want to read the point found by startSpan. This is since startSpan may point to a initial newline.
ForegroundColorSpan foreColour = new ForegroundColorSpan(colour);
// Need a NEW span object every loop, else it just moves the span
// Fix: -1 in startSpan or endSpan, indicates that the indexOf has already searched the entire string with not valid match (Lack of endspan check, occoured because of the inclusion of endTarget, which added extra complications)
if ( (startSpan < 0) || ( endSpan < 0 ) ) break;// Need a NEW span object every loop, else it just moves the span
// Need to make sure that start range is always smaller than end range. (Solved! Refer to few lines above with (!##$%) )
if (endSpan > startSpan) {
//endSpan = startSpan + target.length();
spanRange.setSpan(foreColour, startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Also wannna bold the span too
spanRange.setSpan(new RelativeSizeSpan(size), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanRange.setSpan(new StyleSpan(Typeface.BOLD), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
tv.setText(spanRange);
}
private void changeLineinView_TITLESTYLE(TextView tv, String target, int colour, float size) {
String vString = (String) tv.getText().toString();
int startSpan = 0, endSpan = 0;
//Spannable spanRange = new SpannableString(vString);
Spannable spanRange = (Spannable) tv.getText();
/*
* Had to do this, since there is something wrong with this overlapping the "##" detection routine
* Plus you only really need one title.
*/
//while (true) {
startSpan = vString.substring(0,target.length()).indexOf(target, endSpan-1); //substring(target.length()) since we only want the first line
endSpan = vString.indexOf("\n", startSpan+1);
ForegroundColorSpan foreColour = new ForegroundColorSpan(colour);
// Need a NEW span object every loop, else it just moves the span
/*
if (startSpan < 0)
break;
*/
if ( !(startSpan < 0) ) { // hacky I know, but its to cater to the case where there is no header text
// Need to make sure that start range is always smaller than end range.
if (endSpan > startSpan) {
//endSpan = startSpan + target.length();
spanRange.setSpan(foreColour, startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Also wannna bold the span too
spanRange.setSpan(new RelativeSizeSpan(size), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanRange.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
//}
tv.setText(spanRange);
}
private void spanSetterInView(TextView tv, String startTarget, String endTarget, int typefaceStyle, String fontFamily, int colour, float size, boolean endAtLineBreak) {
String vString = (String) tv.getText().toString();
int startSpan = 0, endSpan = 0;
//Spannable spanRange = new SpannableString(vString);
Spannable spanRange = (Spannable) tv.getText();
while (true) {
startSpan = vString.indexOf(startTarget, endSpan-1); // (!##$%) I want to check a character behind in case it is a newline
endSpan = vString.indexOf(endTarget, startSpan+1+startTarget.length()); // But at the same time, I do not want to read the point found by startSpan. This is since startSpan may point to a initial newline. We also need to avoid the first patten matching a token from the second pattern.
// Since this is pretty powerful, we really want to avoid overmatching it, and limit any problems to a single line. Especially if people forget to type in the closing symbol (e.g. * in bold)
if (endAtLineBreak){
int endSpan_linebreak = vString.indexOf("\n", startSpan+1+startTarget.length());
if ( endSpan_linebreak < endSpan ) { endSpan = endSpan_linebreak; }
}
// Fix: -1 in startSpan or endSpan, indicates that the indexOf has already searched the entire string with not valid match (Lack of endspan check, occoured because of the inclusion of endTarget, which added extra complications)
if ( (startSpan < 0) || ( endSpan < 0 ) ) break;// Need a NEW span object every loop, else it just moves the span
// We want to also include the end "** " characters
endSpan += endTarget.length();
// If all is well, we shall set the styles and etc...
if (endSpan > startSpan) {// Need to make sure that start range is always smaller than end range. (Solved! Refer to few lines above with (!##$%) )
spanRange.setSpan(new ForegroundColorSpan(colour), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanRange.setSpan(new RelativeSizeSpan(size), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanRange.setSpan(new StyleSpan(typefaceStyle), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Default to normal font family if settings is empty
if( !fontFamily.equals("") ) spanRange.setSpan(new TypefaceSpan(fontFamily), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
tv.setText(spanRange);
}
```
The above implementation supports only up to 2 headers (but you can easily modify the regexp to support more than 2 level headers).
It is a series of regexp based text view consisting of two functions for regexp that matches always a line changeLineinView() and changeLineinView_TITLESTYLE()
For multiline spanning spanSetterInView() function deals with it.
So extending it to fit your purpose as long as you have a regexp that doesn't clash with any other syntax would be possible.
Markdownish Syntax:
This is the supported syntax. Can't support full markdown, since this is only a lite hacky implementation. But kind handy for a no frills display that is easy to type on a mobile phone keypad.
# H1 only in first line (Due to technical hacks used)
## H2 headers as usual
## Styling
Like: *italic* **bold** ***bold_italic***
## Classic List
- list item 1
- list item 2
## Nonstandard List Syntax
- list item 1
- list item 2
## Block Quotes
> Quoted stuff
## codes
here is inline `literal` codes. Must have space around it.
```
codeblocks
Good for ascii art
```
Or 4 space code indent like classic markdown.
I can recommend MarkdownView. I use it for loading markdown files from the assets folder.
In case it helps anyone, here's my implementation...
In my layout:
<us.feras.mdv.MarkdownView
android:id="#+id/descriptionMarkdownView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="#id/thumbnailImageView"
app:layout_constraintStart_toEndOf="#id/guidelineStart"
app:layout_constraintEnd_toEndOf="#id/guidelineEnd"
app:layout_constraintBottom_toTopOf="#id/parent"/>
In my Activity:
val cssPath = "file:///android_asset/markdown.css"
val markdownPath = "file:///android_asset/markdown/filename.md"
descriptionMarkdownView.loadMarkdownFile(markdownPath, cssPath)
Take a look at the commonmark-java library.
I haven't tried that myself but I think you might be able to make it work in your case
I followed this post since last Friday and tested many of the Markdown libraries suggested here - this question and these answers were basically the best source about the topic I could find online.
Two of them caught my attention the most, MarkdownView and Markwon, but the former was easier to deal with than the latter and so I used it to empower a Room note taking app by Markdown formatting (which was my main personal goal).
If you want to have a Markdown live preview, you could just use this sample activity provided by the library itself and, among other options, if you need to adapt your own activity to it, I suggest you add the following pieces of code to your project:
build.gradle
implementation 'us.feras.mdv:markdownview:1.1.0'
private MarkdownView markdownView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.markdownView = findViewById(R.id.markdownView);
this.udateMarkdownView();
}
private void updateMarkdownView() {
markdownView.loadMarkdown(note_content.getText().toString());
}
Here you find the sample I put available on GitHub in which you can see a working project apart from the samples the library itself gives us as examples.
If you want to render HTML you can use Html.fromHtml("your string"), for more resources on Strings in Android check this link

How to change some words of a string to BOLD?

This is the string which i am getting from web using json api in my code:
String content = "<strong><em>India is the world’s hub for child sex trafficking</em>
</strong></p>\n<p style=\"text-align: center;\"><strong><em>Nearly 40,000 children are
abducted every year… </em></strong></p>\n<p style=\"text-align: center;\"><strong<em>
Every8 minutes a girl child goes missing in India!</em></strong></p>\n<p><strong>Priti
Pathak</strong></p>\n<p>MEET Shivani Shivaji Roy, Senior Inspector, Crime Branch, Mumbai
Police, as she sets out to confront the mastermind behind the child trafficking mafia,"
In this string whatever the text is in
"<strong><em> TEXT </em></strong>"
I want to display it BOLD. So for the above string
<strong><em>India is the world’s hub for child sex trafficking</em></strong>
will be displayed as
India is the world’s hub for child sex trafficking
. This should happen for entire string. This is the code which i am using:
int startIndex = content.indexOf("<strong><em>")+13; // 13 because <strong><em> has 13 characters
String substring = content.substring(startIndex, startIndex+200);
int subendIndex = substring.indexOf("</em></strong>");
int endIndex = startIndex + subendIndex;
SpannableString s = new SpannableString(content);
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
textview.setText(s, BufferType.SPANNABLE );
This code is working but it is setting bold text only to the text whichever is first within strong tag and not to the rest. Because I am setting bold text only to the first tag.
How should i get the startIndex, endIndex of all the "strong tags" and so i can set
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
to all the texts and also the number of times i would have to set the span.
I though of using regex also but i dont know how to get indexes using it. Any help would be nice. Thankyou !
The easier way is to use the Html class:
s = Html.fromHtml(content);
See the documentation for the details.
This is the simple way
TextView TV = (TextView)findViewById(R.id.Tv);
String Title="I N <big>D</big> I A";
TV.setText(Html.fromHtml(Title));
If you want to only bold some strings then I recommend Html.fromHtml. A short sample:
String str = "<b> bold text</b> unbold one";
textView.setText(Html.fromHtml(str));
Here's a list of html tags supported by textview.
Use regular HTML tags in Strings, This text uses bold and italics by using inline tags such as within the string file.. Refer this link Hope this Link would be helpful for you!

Android - Html.fromHtml handle background color

I have html text that I need to display in TextView. The html may look like this -
<font color="#AFEEEE"><font style="background-color: rgb(255,140,0);">Text with background and color</font></font>
Html.fromHtml doesn't support any attribute other than color for font tag. But we absolutely must show the background. I could write a custom tag handler but the attributes are not passed in, only the tag is passed in.
What is the best way to achieve this ?
NOTE : Cant use Webview.
I tried the code below. If I set raw on the text, it works, but if i process it further and pass it to Html.fromHtml, it doesnt show the background.
public static final String sText =
"Background on <font style=\"background-color: rgb(255,255,0);\">pa</font>rt text only";
Pattern pattern = Pattern.compile(BACKGROUND_PATTERN);
Matcher matcher = pattern.matcher(sText);
SpannableString raw = new SpannableString(sText);
BackgroundColorSpan[] spans =
raw.getSpans(0, raw.length(), BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}
while (matcher.find()) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B),
matcher.start(2), matcher.start(2) + matcher.group(2).length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
sText = raw.toString();
final Spanned convertedHtml =
Html.fromHtml(sText, ig, new myTagHandler());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String str = "<span style=\"background-color:#f3f402;\">" + TEXT TO HIGHLIGHT + "</span>";
textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY));
} else {
String str = "<font color='#f3f402'>" + TEXT TO HIGHLIGHT + "</font>";
textView.setText(Html.fromHtml(str));
}
More - https://stackoverflow.com/a/46035856/3625510
Add your own BackgroundColorSpan as you see fit.
Here is some code that sets such a span on all occurrences of a search term within a TextView:
private void searchFor(String text) {
TextView prose=(TextView)findViewById(R.id.prose);
Spannable raw=new SpannableString(prose.getText());
BackgroundColorSpan[] spans=raw.getSpans(0,
raw.length(),
BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}
int index=TextUtils.indexOf(raw, text);
while (index >= 0) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
prose.setText(raw);
}
So, find your beginning and ending points, create a BackgroundSpan with your desired color, and use setSpan() to apply it.
Note that this assumes that only part of your text needs the background color. If the entire TextView needs the color, go with njzk2's suggestion, and just apply the color to the whole TextView.

Highlight multiple words in a string using SpannableString

I'm using a SpannableString to underline certain words, however, I realized the code I have only highlights the first word if there are multiple words. Not exactly sure how to accomplish highlighting multiple words:
String keyword = "test";
String text = "This is a test to underline the three test words in this test";
SpannableString output = new SpannableString(text);
if (text.indexOf(keyword) > -1)
{
int keywordIndex = text.indexOf(keyword);
int keywordLength = keyword.length();
int start = keywordIndex;
int end = keywordIndex + (keywordLength);
output.setSpan(new UnderlineSpan(), start, end, 0);
}
I was thinking I could split the text at every space and loop through it, but wasn't sure if there was a better way.
I do have this code to highlight multiple words using a regular expression, however, I'm try to avoid regular expressions since it's in an Android app and I'm using it in a ListView and I'm told they are very expensive. Also this code I have only highlight whole words, so using the example text above, if the word "protest" was in the sentence, it wouldn't get highlighted using this code:
Matcher matcher = Pattern.compile("\\b(?:test")\\b").matcher(text);
while (matcher.find())
{
output.setSpan(new UnderlineSpan(), matcher.start(), matcher.end(), 0);
}

How to make part of the text Bold in android at runtime?

A ListView in my application has many string elements like name, experience, date of joining, etc. I just want to make name bold. All the string elements will be in a single TextView.
my XML:
<ImageView
android:id="#+id/logo"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/logo"
android:padding="5dp"
android:textSize="12dp" >
</TextView>
My code to set the TextView of the ListView item:
holder.text.setText(name + "\n" + expirience + " " + dateOfJoininf);
Let's say you have a TextView called etx. You would then use the following code:
final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC); //Span to make text italic
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
etx.setText(sb);
Based on Imran Rana's answer, here is a generic, reusable method if you need to apply StyleSpans to several TextViews, with support for multiple languages (where indices are variable):
void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start = text.indexOf(spanText);
int end = start + spanText.length();
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
Use it in an Activity like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
setTextWithSpan((TextView) findViewById(R.id.welcome_text),
getString(R.string.welcome_text),
getString(R.string.welcome_text_bold),
boldStyle);
// ...
}
strings.xml
<string name="welcome_text">Welcome to CompanyName</string>
<string name="welcome_text_bold">CompanyName</string>
Result:
Welcome to CompanyName
You can do it using Kotlin and buildSpannedString extension function from core-ktx
holder.textView.text = buildSpannedString {
bold { append("$name\n") }
append("$experience $dateOfJoining")
}
The answers provided here are correct, but can't be called in a loop because the StyleSpan object is a single contiguous span (not a style that can be applied to multiple spans). Calling setSpan multiple times with the same bold StyleSpan would create one bold span and just move it around in the parent span.
In my case (displaying search results), I needed to make all instances of all the search keywords appear bold. This is what I did:
private static SpannableStringBuilder emboldenKeywords(final String text,
final String[] searchKeywords) {
// searching in the lower case text to make sure we catch all cases
final String loweredMasterText = text.toLowerCase(Locale.ENGLISH);
final SpannableStringBuilder span = new SpannableStringBuilder(text);
// for each keyword
for (final String keyword : searchKeywords) {
// lower the keyword to catch both lower and upper case chars
final String loweredKeyword = keyword.toLowerCase(Locale.ENGLISH);
// start at the beginning of the master text
int offset = 0;
int start;
final int len = keyword.length(); // let's calculate this outside the 'while'
while ((start = loweredMasterText.indexOf(loweredKeyword, offset)) >= 0) {
// make it bold
span.setSpan(new StyleSpan(Typeface.BOLD), start, start+len, SPAN_INCLUSIVE_INCLUSIVE);
// move your offset pointer
offset = start + len;
}
}
// put it in your TextView and smoke it!
return span;
}
Keep in mind that the code above isn't smart enough to skip double-bolding if one keyword is a substring of the other. For example, if you search for "Fish fi" inside "Fishes in the fisty Sea" it will make the "fish" bold once and then the "fi" portion. The good thing is that while inefficient and a bit undesirable, it won't have a visual drawback as your displayed result will still look like
Fishes in the fisty Sea
if you don't know exactly the length of the text before the text portion that you want to make Bold, or even you don't know the length of the text to be Bold, you can easily use HTML tags like the following:
yourTextView.setText(Html.fromHtml("text before " + "<font><b>" + "text to be Bold" + "</b></font>" + " text after"));
<string name="My_Name">Given name is <b>Not Right</b>Please try again </string>
use "b" tag in string.xml file.
also for Italic "i" and Underline "u".
Extending frieder's answer to support case and diacritics insensitivity.
public static String stripDiacritics(String s) {
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}
public static void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style, boolean caseDiacriticsInsensitive) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start;
if (caseDiacriticsInsensitive) {
start = stripDiacritics(text).toLowerCase(Locale.US).indexOf(stripDiacritics(spanText).toLowerCase(Locale.US));
} else {
start = text.indexOf(spanText);
}
int end = start + spanText.length();
if (start > -1)
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
If you are using the # srings / your_string annotation, access the strings.xml file and use the <b></b> tag in the part of the text you want.
Example:
<string><b>Bold Text</b><i>italic</i>Normal Text</string>
I recommend to use strings.xml file with CDATA
<string name="mystring"><![CDATA[ <b>Hello</b> <i>World</i> ]]></string>
Then in the java file :
TextView myTextView = (TextView) this.findViewById(R.id.myTextView);
myTextView.setText(Html.fromHtml( getResources().getString(R.string.mystring) ));
To better support translations and remove any dependency on length of the string or particular index, you should use android.text.Annotation in you string defined strings.xml.
In your particular case, you can create a string like below
<string name="bold_name_experience_text"><annotation type="bold">name</annotation> \nexpirience dateOfJoininf</string>
or if you want to substitute these in runtime, you can create a string as follow
<string name="bold_name_experience_text"><annotation type="bold">name</annotation> \n%d %s</string>
You must apply this bold_name_experience_text in your text view label. These annotation class spans get added to your string and then you can iterate on them to apply the bold span.
You can refer to my SO answer which shows the Kotlin code to iterate through these spans and apply the bold span
Remember all the above answers has one of the following flows:
They are using some hard-coded index logic which may crash or give wrong results in some other language
They are using hardcode string in Java code which will result in lots of complicated logic to maintain internalisation
Some used Html.fromHtml which can be acceptable answer depending on the use-case. As Html.fromHtml doesn't always work for all types of HTML attributes for example there is not support of click span. Also depending on OEM you might get different rendered TextView

Categories

Resources