Jim Jim - 6 months ago 123
Android Question

How can I add margin or padding between 2 lines in spannable?

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>

What am I doing wrong?

I want to have some padding/margin between

The paddings/margins in my style are big because I wasn't sure if they are applied or not (test numbers)


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>