Teodoro Teodoro - 1 year ago 245
Android Question

Different font size of strings in the same TextView

I have a

inside with a number (variable) and a
, how can I give the number one size larger than the string?
the code:

TextView size = (TextView)convertView.findViewById(R.id.privarea_list_size);
if (ls.numProducts != null) {
size.setText(ls.numProducts + " " + mContext.getString(R.string.products));

I want ls.numproducts has a size different from the rest of the text. How to do?

Answer Source

Use a Spannable String

 String s= "Hello Everyone";
 SpannableString ss1=  new SpannableString(s);
 ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
 ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
 TextView tv= (TextView) findViewById(R.id.textview);

Snap shot

enter image description here

You can split string using space and add span to the string you require.

 String[] each;
 String s= "Hello Everyone";  
 for(int i=0;i<s.length();i++)
     each = s.split("\\s");         

Now apply span to the string and add the same to textview.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download