Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spannable on android for textView

Tweet o = tweets.get(position);  TextView tt = (TextView) v.findViewById(R.id.toptext); //TextView bt = (TextView) v.findViewById(R.id.bottomtext);           EditText bt =(EditText)findViewById(R.id.bottomtext); bt.setText(o.author); Spannable spn = (Spannable) bt.getText(); spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC) , 0, 100, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    //bt.setText(o.author);     tt.setText(o.content); 

I'm setting twitter data in my Android application. I want to make the font bold and italic using Spannable but it does not work, giving an error. How can I do it?

like image 814
baran Avatar asked Feb 12 '13 19:02

baran


People also ask

How do I use Spannable text on android?

To apply a span, call setSpan(Object _what_, int _start_, int _end_, int _flags_) on a Spannable object. The what parameter refers to the span to apply to the text, while the start and end parameters indicate the portion of the text to which to apply the span.

What is Spannable string in android?

↳ android.text.SpannableString. This is the class for text whose content is immutable but to which markup objects can be attached and detached. For mutable text, see SpannableStringBuilder .

What is a difference between Spannable and string?

A Spannable allows to attach formatting information like bold, italic, ... to sub-sequences ("spans", thus the name) of the characters. It can be used whenever you want to represent "rich text". The Html class provides an easy way to construct such text, for example: Html.


2 Answers

I want to make the font bold and ıtalic with spannable

for this u will need to make o.content text as SpannableString then set it to TextView as :

SpannableString spannablecontent=new SpannableString(o.content.toString()); spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),                           0,spannablecontent.length(), 0); // set Text here tt.setText(spannablecontent); 

EDIT : you can also use Html.fromHtml for making text Bold and Italic in textview as :

tt.setText(Html.fromHtml("<strong><em>"+o.content+"</em></strong>")); 
like image 104
ρяσѕρєя K Avatar answered Sep 21 '22 13:09

ρяσѕρєя K


The easy way to create a spannable text bold and italic to set into your TextView is using the method Html.fromHtml():

and using the html elements <b> and <i>

myTextView.setText(Html.fromHtml("<b><i>my text bold and italic!</i></b>")); 

or the html elements <strong> and <em>

   myTextView.setText(Html.fromHtml("<strong><em>my text bold and italic!</em></strong>")); 
like image 44
Jorgesys Avatar answered Sep 22 '22 13:09

Jorgesys