Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any example about Spanned and Spannable text

I'm struggling with using EditText and Spannable text object, These days, I've read API documents around ten times, even I'm not certain that I understand correctly. So I'm looking for a kind of example which show me how to utilize EditText and Spannable.

like image 941
skysign Avatar asked Jan 29 '10 04:01

skysign


People also ask

What is spanned text?

Spans are powerful markup objects that you can use to style text at a character or paragraph level. By attaching spans to text objects, you can change text in a variety of ways, including adding color, making the text clickable, scaling the text size, and drawing text in a customized way.

What is a Spannable string?

Text styling is one of the important aspects when it comes to enhancing the UI of an Android application. In Android, we can change the size, color, weight, style, etc of a text and make the text more attractive and appealing.

How do I change text color in Spannable Android?

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text2); // It is used to set foreground color. ForegroundColorSpan green = new ForegroundColorSpan(Color. GREEN);

How do I remove underline from spannableString Android?

@Override public void updateDrawState(@NonNull TextPaint ds) { // super. updateDrawState(ds); } },0, spannableString. length(), SPAN_EXCLUSIVE_EXCLUSIVE); The updateDrawState overrides the updateDrawState in the ClickableSpan class in Android, and by not calling super.


1 Answers

Since you don't specify what you can't grasp from the API it's hard to answer your questions (short answer: rewrite your question to a specific questions rather than a general one).

A typical Spannable-example is something like this to turn selected text in an EditText into Italic:

Spannable str = mBodyText.getText();  if(mBodyText.getSelectionEnd() > mBodyText.getSelectionStart())    str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),                         mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),                         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); else   str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),               mBodyText.getSelectionEnd(),               mBodyText.getSelectionStart(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

This is cut and pasted from something else, so your direct-pastability might have suffered, but it at least shows a working example of a Spannable (in this case a StyleSpan). In the API you can find the other types of Spans (notably ImageSpan, which is a common questions among newly converted droiders).

like image 168
andy Avatar answered Sep 22 '22 15:09

andy