Different Font Size of Strings in the Same Textview

Different font size of strings in the same TextView

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

Snap shot

Sample Image

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

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

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

Android TextView making the font different size per line

It can be done in 1 TextView with SpannableString

From https://developer.android.com/reference/android/text/style/RelativeSizeSpan

You can use:

SpannableString string = new SpannableString("Text with relative size span");
string.setSpan(new RelativeSizeSpan(1.5f), 10, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Where 1.5f would be an increase of 50%, 10 the starting index and 24 the end index

public void setSpan (Object what, 
int start,
int end,
int flags)

Another example here Different font size of strings in the same TextView too

How to set two different sizes for single textview in android


Thank you very much for your support guys...This helps me to get two different font size in single textview.

String Amount="Rs.109";
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
Spannable span = new SpannableString(Amount);
span.setSpan(new RelativeSizeSpan(0.75f), 0, 3,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(span);

TextView with different textSize

Try

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Multiple textsize in a single textview in android

No, it is not possible from the XML, but you can do it in JAVA.

Check out this and this.

String with multiple fonts and textSize in the same textView

You need to create a String with HTML tags and set this String to TextView as Html.fromHtml like below:

((TextView) findViewById(R.id.txt)).setText(
Html.fromHtml("<Font color=\'" + "#457548"
+ "'\" >" + "your text"
+ "</Font>"), TextView.BufferType.SPANNABLE)

More information go to: how-display-html-android

Android - Align different font-sizes on same baseline

Change the height of both TextView elements to wrap_content. By default, a horizontal LinearLayout will automatically align the baseline of TextView children.

If you're currently using match_parent for height in order to have a full-height background color, you'll have to think of a different way to accomplish that. You could, perhaps, use a background color on the LinearLayout and then only specify a background color on the larger TextView; this will give the same effect you have today.



Related Topics



Leave a reply



Submit