Underline a word within a Textview

Asked

Viewed 2,390 times

1

As I can underline, leave in bold/italic a single word within a TextView?

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 Casa Dado"

I must use some html code?

2 answers

4

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

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

This method is obsolete from the 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

0


There are a few options you can join for a Textview, in which I will give 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 text used 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 resources of string provide strings text for the application with style and optional text formatting.

xml string.

<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));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.