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)
Related
I use TypedValue.applyDimension and displayMetrix.scaledDensity to set text size in the TextView on my Xiaomi Redmi 5 plus and get surprizing results: text in the TextView is much larger than the one (same sp value) set in layout attribute. Here's my layout:
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center|start"
android:layout_margin="4dp"
android:text="Some Text"
android:textSize="18sp" />
And here is how I change text size:
TextView titleView = itemView.findViewById(R.id.my_text_view);
titleView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18,
getResources().getDisplayMetrics()));
I also tried to use formula as follows:
(int) getResources().getDisplayMetrics().scaledDensity * 18
On the emulator, I get the right text size, so I am not sure how to reproduce this. If someone manages to do that, please, leave comments for me and others to understand what is actually wrong.
Thanks
So, I opend TextView source code and found out that TextView.setTextSize accepts sp, not px. So the answer is not to convert sp to px, but to use sp by default, or, if you need to support several size units, use setTextSize(int unit, float size)
I'm using android graphview 3.1.1. I am trying to set an axis label, a label identifying what the axis is.
The only thing I've found close to this in Android GraphView is setting individual tick-mark labels as withgraphView.setHorizontalLabels(horlabels). Is it possible to set the entire label?
Example here, the axis labels are Month and Weight in Kg.
I have had a similar problem, so I set a by -90° rotated TextView beside my LinearLayout where i draw the graph.
<TextView
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:id="#+id/test"
android:textColor="#FFFFFF"
android:gravity="center"
android:rotation="-90"
android:textSize="12dp"
android:text="°C"/>
The problem with this method: The width of my rotated TextView stays the same like if's not rotated. So there is a huge amount of space to the left and right if you use a long text. If you shorten the layout_widht there is a line break.
For short text it looks nice: http://i.stack.imgur.com/KWMk8.jpg
there is no build-in feature for that, but you can achieve this by nesting the GraphView-View in layouts with TextViews
There are no ways provided to set the entire label. You may have to do this with TextViews.
I think the solution will be quite obvious, but as long as I'm rookie in android forgive me :)
I want to place some picture on the right upper side of the view, and a textview, which will fit the rest of the view. Here's what I've done:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/logo_in_listing"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_alignParentRight="true"
android:layout_marginRight="11dip"
android:layout_marginTop="11dip"
android:background="#drawable/border_for_imageview"
android:src="#drawable/facebook_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/someString"
android:id="#+id/textview1"
android:layout_toLeftOf="#id/logo_in_listing"
android:layout_margin="3dip"/>
</RelativeLayout>
Here's what I get:
My questions:
1) How to make the textview fit also the lower part of imageview? I don't want that space to be left unused.
2) As you see, I've used dips for placing ImageView. What would you recommend in this case? I mean, how to write in a way, that it will be same for all screen sizes? Or maybe dip is OK?
You have to use Spanned interface. Here's a good example
1) How to make the textview fit also the lower part of imageview? I
don't want that space to be left unused.
Flow around text isn't possible with currently available layouts. However you can display content as HTML in a WebView or even spanned-HTML in a TextView.
2) As you see, I've used dips for placing ImageView. What would you
recommend in this case? I mean, how to write in a way, that it will be
same for all screen sizes? Or maybe dip is OK?
Dip is for device independent pixels. On different screen sizes, 1 Dip will take different number of pixels, but visually, 1 dip will appear almost same to eye on all screens.
If You don´t want to left space, You could use spannables. For example:
int TEXT_START = 0;
int TEXT_END = yourTextView.length();
Spannable span = (Spannable) yourtextview.getText();
span.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), TEXT_START, TEXT_END,
0);
For Your second Question: use dp instead of dip.
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.
TableLayout relativeLayout = (TableLayout)findViewById(R.id.ll1);
TableRow row =new TableRow(this);
Button button ;
int counter = 0;
for(int i = 0;i<=13;i++){
counter++;
button = new Button(this);
button.setText(counter);
row.addView(button);
}
relativeLayout.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
relativeLayout.computeScroll();
XML File
<TableLayout android:id="#+id/ll1" android:shrinkColumns="true"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="#id/button1"
xmlns:android="http://schemas.android.com/apk/res/android"></TableLayout>
But whenever i execute my application it just move my buttons in a single row and after 5 buttons all the buttons move out from the screen. i dont' want to add more rows as i don't want to hard-code anything in my code like placing some code if (counter%5==0) then do this and that.
Is there any other way to calculate the width that if the buttons are equivalent to the width of screen then do this or that or any other way like any property in table Layout which simply wrap content of a row?
Yes there is a way to get the exact width of any component in your app. Look at my answer to my question asked a while ago here. As you can see, I instantiate a Paint object, but that's not necessary unless you wish to get the width of the text itself. However you must use the density multiplier:
float scale = getContext().getResources().getDisplayMetrics().density;
That will convert any function that gets the width of a component (in this case, your button) to the actual pixel width on any given devices. So you would get the button width (via invoking the getWidth() function on your Button object) and then multiply by that scaling factor.
Use FlowLayout instead:
http://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/
GIT project:
https://gist.github.com/1073863
Using Contex's method getResources() you can access all the resources for your application's package. The method returns Resources and you can see here all properties that are available.