mohsen
mohsen

Reputation: 461

How to give 5dp linespace to TextView programmatically

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?

Upvotes: 14

Views: 15156

Answers (2)

solidogen
solidogen

Reputation: 649

If pulling a dimen resource:

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

Upvotes: 3

erad
erad

Reputation: 1786

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

Upvotes: 28

Related Questions