Different line font sizes in the same TextView

I have a textView inside with a number (variable) and string , how can I give number one size larger than 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 the ls.numproducts size to be different from the rest of the text. How to make?

+111
android string android-textview text-size
May 02 '13 at 9:58
source share
7 answers

Use 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); tv.setText(ss1); 

Snapshot

enter image description here

You can split the string using a space and add span to the desired string.

  String s= "Hello Everyone"; String[] each = s.split(" "); 

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

+287
May 02 '13 at 10:10
source share

In case you are wondering how you can set several different sizes in the same textual representation, but using absolute rather than relative size, you can achieve this by using AbsoluteSizeSpan instead of RelativeSizeSpan .

Just get the pixel dimension of the desired text size

 int textSize1 = getResources().getDimensionPixelSize(R.dimen.text_size_1); int textSize2 = getResources().getDimensionPixelSize(R.dimen.text_size_2); 

and then create a new AbsoluteSpan based on text

 String text1 = "Hi"; String text2 = "there"; SpannableString span1 = new SpannableString(text1); span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE); SpannableString span2 = new SpannableString(text2); span2.setSpan(new AbsoluteSizeSpan(textSize2), 0, text2.length(), SPAN_INCLUSIVE_INCLUSIVE); // let put both spans together with a separator and all CharSequence finalText = TextUtils.concat(span1, " ", span2); 
+94
Jan 27 '15 at 15:58
source share

You can do this using the html string and setting the html to Textview using txtView.setText(Html.fromHtml("Your html string here"));

For example:

 txtView.setText(Html.fromHtml("<html><body><font size=5 color=red>Hello </font> World </body><html>"));` 
+9
May 2 '13 at 10:01
source share

Method 1

 public static void increaseFontSizeForPath(Spannable spannable, String path, float increaseTime) { int startIndexOfPath = spannable.toString().indexOf(path); spannable.setSpan(new RelativeSizeSpan(increaseTime), startIndexOfPath, startIndexOfPath + path.length(), 0); } 

using

 Utils.increaseFontSizeForPath(spannable, "big", 3); // make "big" text bigger 3 time than normal text 

enter image description here

Method 2

 public static void setFontSizeForPath(Spannable spannable, String path, int fontSizeInPixel) { int startIndexOfPath = spannable.toString().indexOf(path); spannable.setSpan(new AbsoluteSizeSpan(fontSizeInPixel), startIndexOfPath, startIndexOfPath + path.length(), 0); } 

using

 Utils.setFontSizeForPath(spannable, "big", (int) textView.getTextSize() + 20); // make "big" text bigger 20px than normal text 

enter image description here

+2
Nov 17 '17 at 2:09 on
source share

Try spannableStringbuilder . Using this, we can create a string with several font sizes.

+1
May 02 '13 at 10:04
source share

I wrote my own function that takes 2 lines and 1 int (text size)

The full text and part of the text you want to resize.

It returns a SpannableStringBuilder, which you can use in a text view.

  public static SpannableStringBuilder setSectionOfTextSize(String text, String textToChangeSize, int size){ SpannableStringBuilder builder=new SpannableStringBuilder(); if(textToChangeSize.length() > 0 && !textToChangeSize.trim().equals("")){ //for counting start/end indexes String testText = text.toLowerCase(Locale.US); String testTextToBold = textToChangeSize.toLowerCase(Locale.US); int startingIndex = testText.indexOf(testTextToBold); int endingIndex = startingIndex + testTextToBold.length(); //for counting start/end indexes if(startingIndex < 0 || endingIndex <0){ return builder.append(text); } else if(startingIndex >= 0 && endingIndex >=0){ builder.append(text); builder.setSpan(new AbsoluteSizeSpan(size, true), startingIndex, endingIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }else{ return builder.append(text); } return builder; } 
+1
Feb 13 '18 at 12:25
source share
 private SpannableStringBuilder SpannableStringBuilder(final String text, final char afterChar, final float reduceBy) { RelativeSizeSpan smallSizeText = new RelativeSizeSpan(reduceBy); SpannableStringBuilder ssBuilder = new SpannableStringBuilder(text); ssBuilder.setSpan( smallSizeText, text.indexOf(afterChar), text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); return ssBuilder; } ------------------------ TextView textView =view.findViewById(R.id.textview); String s= "123456.24"; textView.setText(SpannableStringBuilder(s, '.', 0.7f)); 

---------------- Result ---------------

Result:

12345.24

+1
Jun. 07 '18 at 9:38
source share



All Articles