I have a TextView which displays a long text. I want to give some space between lines like in CSS with line-height property. How can I do it?
You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.
If you want padding between text, try LineSpacingExtra="10sp"
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:lineSpacingExtra="10sp"/>
you can look into android:lineSpacingExtra and apply it to your XML
Additional Info is on this page
or the related method public void setLineSpacing (float add, float mult)
Additional Info here
This supplemental answer shows the effect of changing the line spacing.
You can set the multiplier and/or extra spacing with
textView.setLineSpacing(float add, float mult)
Or you can get the values with
int lineHeight = textView.getLineHeight();
float add = tvSampleText.getLineSpacingExtra(); // API 16+
float mult = tvSampleText.getLineSpacingMultiplier(); // API 16+
where the formula is
lineHeight = fontMetricsLineHeight * mult + add
The default multiplier is 1 and the default extra spacing is 0.
Adding android:lineSpacingMultiplier="0.8" can make the line spacing to 80%.
You can use TextView.setLineSpacing(n,m) function.
You can either use lineSpacingExtra or lineSpacingMultiplier in your XML file.
lineSpacingExtra add extra spacing between lines of text of TextView
<TextView
android:lineSpacingExtra="4dp" />
lineSpacingMultiplier works as scale factor for height of line space:
<TextView
android:lineSpacingMultiplier="0.8" />
In other words, each line height will be height * multiplier + extra.
You can use 2 attrs
1. lineSpacingExtra:
it use for dp spacing
android:lineSpacingExtra="4dp"
2. lineSpacingMultiplie:
it use for relative scale
android:lineSpacingMultiplier="0.8"
As an extended answer to above solutions
Remember you can add <item name="android:lineHeight">16sp</item> for directly setting the line heights but as per the docs above line works like this -
Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.
<attr name="lineHeight" format="dimension" />
So be sure to use either lineSpacingExtra & lineSpacingMultiplier or lineHeight and not both.
As of 16/11/2021,
I use this line to increase the line space height:
android:lineHeight="25dp"
For me the other answers weren't helpful because maybe they updated the attribute and quite a lot of stuff changed in this version.
Related
can I know what is the difference between lineHeight and lineSpacingExtra in android xml. I tried to compare both, but I got the different result (I need the lineHeight function, however it is only supported in API 28).
Here is part of my code:
left:
android:textSize="14sp"
android:lineSpacingExtra="6dp"
right:
android:textSize="14sp"
android:lineHeight="20dp"
Result:
Any solution for this?
Thank you.
You mentioned you want to set lineHeight in pre-API 28, an alternative approach would be to just set a small lineSpacingExtra / lineSpacingMultiplier value (as you have shown). Otherwise, there are many solutions to setting a line height yourself in this related question.
I'll briefly cover the differences below, with my summary and the official documentation.
android:lineHeight is the total height of each line. This includes the text, as well as any padding on the top and bottom.
Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.
android:lineSpacingExtra is the additional spacing added after each line of text (except the last one).
Extra spacing between lines of text. The value will not be applied for the last line of text.
Finally, you may be interested in android:lineSpacingMultiplier. This is similar to lineSpacingExtra, but with a multiplier value of the current height (e.g. 1.2) instead of a fixed value.
Extra spacing between lines of text, as a multiplier. The value will not be applied for the last line of text.
Further information (besides the included quotas) is available in the TextView documentation.
MaterialTextView (MTV) includes the ability to set the android:lineHeight.
And if you use the app: prefix, it is backwards compatible until SDK-Version 21.
Btw., under some circumstances (i believe its depending on your style, but am not sure about it), standard TextViews are automatically replaced with MTVs. (Check your LayoutInspector, you might already use them without knowing it)
In case you're still wondering about the differences, there's an excellent talk by the android team about text in general; which should solve your question at ~16:50.
android:lineHeight defines Explicit height between lines of text. while android:lineSpacingExtra defines Extra spacing between lines of text
you can read more android_documentation_for_text_arributes
android:lineHeight
is used to specify the total height for single line of text.
android:lineSpacingExtra
is used to add extra spacing between 2 lines of text in addition to the default line height used by android.
In order to replicate lineHeight functionality with API level < 28, set the lineSpacingMultiplier to 0 and set the lineSpacingExtra to the lineHeight that you want to use
android:lineSpacingMultiplier="0"
android:lineSpacingExtra="<enter line height value here in dp>"
I have a TextView which should have 10dp padding in the top and bottom, and 30dp on the left. I then tried:
android:padding="10dp"
android:paddingLeft="30dp"
That's what I'd do in CSS. But it doesn't work.
Regardless of the order, my left padding is 10dp. Are Android style attributes not overridden in the order they appear? If so, is there any list of priorities?
Ex: android:padding always overrides android:paddingLeft
Overriding is an important feature in CSS which saves a lot of coding, so I wanted to know if that's possible in Android.
Maybe I just couldn't find the answer with the right terms. If someone could point me where to understand it I'd be very appreciated.
The Android layout system is different from CSS. It does not care about the order of the properties, and padding always overrides paddingLeft, paddingRight, etc.
You can find the relevant source code in View.java here (padding is set) and here (if padding is set it is used, even if paddingLeft is set).
This is the most important snippet:
if (padding >= 0) {
leftPadding = padding;
topPadding = padding;
rightPadding = padding;
bottomPadding = padding;
mUserPaddingLeftInitial = padding;
mUserPaddingRightInitial = padding;
}
As I understand from the source code,
when you set the padding attribute it can be overridden by other padding attributes.
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?
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 pulling a dimen resource:
setLineSpacing(resources.getDimension(R.dimen.dp5), 1.0f)
I Displayed my program's output via TableRow in TableLayout using array of Textview.Actually the output is of 2d array.
I displayed every element of 2d array without any problems.
But what the problem is I cant have space between TextViews(Columns).
Below is the image which explains this clearly.I want to do this programmatically
Please help me to solve this
Remember that almost any property that can be set on your XML layout, can also be set programmatically!
The way to do it is:
view.setPadding(0,padding,0,0);
This will set the top padding to padding-pixels. If you want to set it in dp instead, you can do a conversion:
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
I go this from here .
You can set padding programmatically on the textview by using this method:
txt.setPadding(left, top, right, bottom);
set integer value for left,top,right,bottom according to your condition.Hope it will help...
I'm adding line spacing in my textview which spans multiple lines.
What's the difference between android:lineSpacingExtra and android:lineSpacingMultiplier?
lineSpacingExtra with 2dp worked fine for me but I was wondering what the Multiplier does instead?
The difference is that android:lineSpacingExtra add extra spacing between lines of text of TextView and android:lineSpacingMultiplier work as scale factor for height of line space. in other words, each line height will be height*multiplier + extra
It's rather simple: one is additive and one is multiplicative.
If you have a default line spacing of LINE_SPACING and use:
float x = 2;
float y = 1.5;
setLineSpacing(x, y);
The resulting line spacing will be 1.5*LINE_SPACING + 2
It is important to note that the multiplication happens first! This follows the conventional order of operations (multiplication before addition).
See the docs here: http://developer.android.com/reference/android/widget/TextView.html#setLineSpacing(float, float)
In the future, it might be wise to look up such documentation first. ;)