How to Set The Font Style to Bold, Italic and Underlined in an Android Textview

How to set the font style to bold, italic and underlined in an Android TextView?

I don't know about underline, but for bold and italic there is "bolditalic". There is no mention of underline here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle

Mind you that to use the mentioned bolditalic you need to, and I quote from that page

Must be one or more (separated by '|') of the following constant values.

so you'd use bold|italic

You could check this question for underline: Can I underline text in an android layout?

How to set TextView textStyle such as bold, italic

textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);

To keep the previous typeface

textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)

Set bold and italic style in a textview with custom font (Programatically)

You need to have a font which is both bold and italic, that's how fonts work. Unfortunately Android cannot just combine those as it's not that easy, so you need to have your own file.

how to make a specific text on TextView BOLD

Just build your String in HTML and set it:

String sourceString = "<b>" + id + "</b> " + name; 
mytextview.setText(Html.fromHtml(sourceString));

Set some text bold in textview, but not everything

From the Android docs: https://developer.android.com/reference/android/text/style/StyleSpan

 SpannableString string = new SpannableString("Bold and italic text");
string.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new StyleSpan(Typeface.ITALIC), 9, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Then call textView.setText(string); like you normally would.

EDIT: You can also try using
HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);
which also returns a Spanned of its own.

However, if you're putting this html as a string resource, you will need to escape some XML characters.

Android - Set EditText Bold / Italic / Underline for the future text

You need to use spans.

editText.doAfterTextChanged() {...
when(selectedStyle) {
Italic -> {
val spannable = SpannableString(text)
spannable.setSpan(Spans.Italic, styleStart, text.length, Spannable.EXCLUSIVE_EXCLUSIVE)
}
}
}

This article should help you
https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568

And this question should help you with the text listener
android on Text Change Listener

When the user press the button for changing the style then save the text lenght so in the TextWatcher after the text change you apply the style selected.

Maybe you should consider looking for some WYSIWYG library around.

How do I set different font sizes to a string (that is highlighted, underlined) in a textview

You can use text Spans to apply formatting to parts of a string.

SpannableString spannablecontent=new SpannableString(content.toString());
//set a Style Span to apply Typeface style
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
0,spannablecontent.length(), 0);

You can set RelativeSizeSpan to change the text sizes if required.

And then finally, set the spanned string in a TextView.

mTextView.setText(spannablecontent);


Related Topics



Leave a reply



Submit