Another option is to write directly inside your text and apply the HtmltextView transformation :
textView.setText(Html.fromHtml("Hola <b>mundo</b>, me llamo <b>Pepe</b>"));
The third option is using SpannableString and defining a StyleSpan with style boldfor the words in bold, in this option you have to calculate the start and end index of the word(s) you want to apply the style to:
TextView textView = (TextView) findViewById(R.id.textView);
SpannableString miTexto = new SpannableString("Hola mundo, me llamo Pepe");
StyleSpan boldSpan1 = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
miTexto.setSpan(boldSpan1, 5, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
miTexto.setSpan(boldSpan2, 20, miTexto.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(miTexto);
obtaining with the 3 options the same result:
Using an .xml or a .json you can add html code and parse the value so that it is represented in your view, in the case of an xml don't forget to add the container<![CDATA
Using an XML
<texto><![CDATA[Hola <b>mundo</b>, me llamo <b>Pepe</b>]]></texto>
Using a .json
{
"texto": "Hola <b>mundo</b>, me llamo <b>Pepe</b>",
}
Well, I see that all the answers that they give you are making use of HTMLwhich are a very good choice, in this answer I suggest that you make use of SpannableStringBuilder in case one day you want to apply it in another way :D
final SpannableStringBuilder texto= new SpannableStringBuilder("Tu texto");
final StyleSpan letraEnNegrita= new StyleSpan(android.graphics.Typeface.BOLD); // Para hacer negrita
texto.setSpan(letraEnNegrita, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // Convierte los primeros 4 caracteres en negrita, tu puedes decirle cuantos caracteres :)
tuTextview.setText(texto);
There are several ways to do this (two are similar to this answer but now you would use
<b>
).One option is loading the text from the text from
Strings.xml
and usingHtml.fromHtml()
, in this case it is very important to use theCDATA
,and loading the text
TextView
by:You can also load the unused text
CATA
, for this case the getText() method is used , we define the string inside this wayStrings.xml
:and load it using the getText() method :
Another option is to write directly inside your text and apply the Html
textView
transformation :The third option is using SpannableString and defining a StyleSpan with style
bold
for the words in bold, in this option you have to calculate the start and end index of the word(s) you want to apply the style to:obtaining with the 3 options the same result:
Using an .xml or a .json you can add html code and parse the value so that it is represented in your view, in the case of an xml don't forget to add the container
<![CDATA
Using an XML
Using a .json
You can use HTML tags for this purpose. For example, you can perform:
You can make use of the object
Html
and its methodfromHtml
to bold the necessary words in yourstring
Well, I see that all the answers that they give you are making use of
HTML
which are a very good choice, in this answer I suggest that you make use of SpannableStringBuilder in case one day you want to apply it in another way :DCheck out my class, it extends from TextView allowing you to add HTML and the fade-in-out effect when changing the text value:
To add it to your XML:
Programmatically adds text to the object.