Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 872
mohsen Avatar asked Sep 14 '14 04:09

mohsen


People also ask

How do I assign text to TextView?

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.

How do I add a newline to a TextView in android?

for the new line in TextView just add \n in middle of your text it works..

How do I reduce line spacing in TextView?

You can use TextView. setLineSpacing(n,m) function. Show activity on this post. You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.

How do you put a space between lines on android?

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().


1 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" />
like image 153
erad Avatar answered Oct 16 '22 13:10

erad