I want to change the linespace of a TextView programatically. I searched and I found setLineSpacing
. The problem is this, it gets two parameters, I've tried so many values but I couldn't get the result I wanted. I just need to give the TextView 5dp linespace, what should I put in the method to give it 5 dp linespace?
Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.
for the new line in TextView just add \n in middle of your text it works..
You can use TextView. setLineSpacing(n,m) function. Show activity on this post. You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.
Just we need to add an attribute with your TextView that give the spacing with lines. These are –android:lineSpacingExtra, android:lineSpacingMultiplier – use these two attributes with your TextView in XML file. or Programatically call setter method of that attribute textView. setLineSpacing().
Why can't you use setLineSpacing?
That is exactly what I'd use.
Based on the Android Documentation:
public void setLineSpacing (float add, float mult)
Each line will have its height multiplied by mult and have add added to it.
So here's what you may choose to do:
myTextView.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5.0f, getResources().getDisplayMetrics()), 1.0f);
Or you can modify the XML Layout of android:lineSpacingExtra
. (See Android Documentation.)
<TextView
android:id="@+id/txtview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:lineSpacingExtra="5dp" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With