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.
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.
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.
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text2); // It is used to set foreground color. ForegroundColorSpan green = new ForegroundColorSpan(Color. GREEN);
@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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With