2

I am using a spannable to create a string in 2 lines where each line has different font.
It works fine except one thing.
How can I add some margin between the first string with the second string?

So I do:

String a = "First line";  
String b = "Second line";  
SpannableString spannableName = new SpannableString(a + "\n" + b);  
spannableName.setSpan(new TextAppearanceSpan(activity, R.style.my_style),0,a.length(), 0);  

and my_style

<style name="my_style">
        <item name="android:layout_marginBottom">100dp</item>
        <item name="android:paddingBottom">10dp</item>
        <item name="android:textSize">@dimen/size</item>
        <item name="android:textColor">@color/mycolor</item>
    </style>  

What am I doing wrong?

I want to have some padding/margin between a and b.
The paddings/margins in my style are big because I wasn't sure if they are applied or not (test numbers)

Jim
  • 18,826
  • 34
  • 135
  • 254

2 Answers2

4

Here is how you can do it with RelativeSizeSpan:

String a = "First line";
String b = "Second line";
SpannableString spannableName = new SpannableString(a + "\n" + b);
spannableName.setSpan(new TextAppearanceSpan(activity, R.style.my_style),0,a.length(), 0);
spannableName.setSpan(new RelativeSizeSpan(1.5f), a.length(), a.length() + 1, 0);

RelativeSizeSpan will make the "\n" character 1.5 bigger than the "a" and "b" string.

Herve Thu
  • 1,207
  • 1
  • 12
  • 10
3

That has to be a property of the UI control you are using.

I assume you are setting the text into a TextView. You can use textView.setLineSpacing(addvalue, multiplyValue). This will add addValue units to the spacing and multiply it multiplyValue times.

Similarly, if you want it set through style you can add these values to your style:

<item name="android:lineSpacingExtra">SoemDPValue</item>
<item name="android:lineSpacingMultiplier">someNumber</item>
N.T.
  • 2,601
  • 1
  • 14
  • 20