Underline a word inside a TextView [duplicate]

1

How can I emphasize, leave a single word inside a TextView in bold / italic?

For example:

  <TextView
            android:text="Abelha Barco Casa Dado"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/textView

            android:textColor="#ffffff"/>

"Bee Boat House Dice"

Should I use some html code?

    
asked by anonymous 22.12.2016 / 20:50

2 answers

0

There are a few options you can join for a TextView , in which I'll give you some examples .

SpannableStringBuilder

This is the class whose content and markup can both be changed, considering it to be a more native approach.

SpannableStringBuilder texto = new SpannableStringBuilder();
        texto.append("Jeitos de colocar ");
        int start = texto.length();
        texto.append("o texto negrito");
        texto.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, 
        texto.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        texto.append(" em um TextView");

        textView.setText(texto); 

Html.fromHtml

Use some of the tags HTML to format used text in a TextView .

String texto = "Jeitos de colocar <b>o texto negrito</b> em um TextView";

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
   textView.setText(Html.fromHtml(texto,Html.FROM_HTML_MODE_LEGACY));
} else {
   textView.setText(Html.fromHtml(texto));
}

Parameters:

public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;

String Resource

The string features provide text strings for the application with style and optional text formatting.

string.xml

<resources>
    ...
    ...
    <string name="TextoHtml"> 
     Jeitos de colocar 
     <b>o texto negrito</b> 
     em um TextView
    </string>
</resources>

main.java

textView.setText(getString(R.string.TextoHtml));
    
23.12.2016 / 02:19
4

You can use Html.fromHtml - > Spanned fromHtml (String source)

Spanned text = Html.fromHtml("Abelha <b>Barco</b> Casa Dado");
textView.setText(text);

This method is obsolete from API Level 24 , so you should implement something like:

if (Build.VERSION.SDK_INT >= 24)
{
    textView.setText(Html.fromHtml("Abelha <b>Barco</b> Casa Dado",Html.FROM_HTML_MODE_LEGACY));    
}
else
{
    textView.setText(Html.fromHtml("Abelha <b>Barco</b> Casa Dado"));
}
  

FROM_HTML_MODE_LEGACY

    
22.12.2016 / 21:29