Home » Android » android – How to give 5dp linespace to TextView programmatically

android – How to give 5dp linespace to TextView programmatically

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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" />

Answer:

If pulling a dimen resource:

setLineSpacing(resources.getDimension(R.dimen.dp5), 1.0f)