I have a TextView with few lines and i need to calculate just the height of one line, not the total TextView height.
TextView's height divided by TextView's rows should give you the height of one row hence every line has the same height with one text size.
Related
I'm using a (horizontal) PagerView which shows 1 page on the screen, the user can switch pages by swiping left or right.
Every page holds several views in a RelativeLayout, one of them is a description text in a TextView (its size is calculated with weight).
I want the description text to be in the same font size for all pages.
This needs to be according to the longest description (the TextViews in all the pages are in the same height).
I have an array of all the descriptions in advance but I don't have rendered TextViews since they are in different pages and android won't draw it until scrolled to the page.
Any idea how the correct font size could be calculated?
Found the solution so just sharing.
This is easily implemented using StaticLayout. When creating StaticLayout you assign its width and can check its height (it will automatically start the text in a new line if it's too long).
Just iterate over your "texts array", for every line of text create a StaticLayout with your desired width and make sure it's height fits inside your TextView height, if not, decrease the font size and recheck. You'll eventually get the largest font size that fits the TextView size.
Get the 1st textview text size & typeface. Then set that size and typeface to other textviews. Or you can get height & width of the textview & set that to others.
float size = textView1.getTextSize(); //get the size of the textview text
textView2.setTextSize(size) //set the size for other textview's texts
//if you want look in same size, all text must have same TypeFace(Font). To do that use.
textView1.getTypeFace();
textView2.setTypeFace();
//or you can get hight & width of the textview.
textView.setText("GetHeight");
textView.measure(0, 0); //must call measure!
textView.getMeasuredHeight(); //get height
textView.getMeasuredWidth(); //get width
//set textview height & width
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
params.width = 100;
textView.setLayoutParams(params);
Consider 4 TextViews inside a horizontal layout each of which has Layout width of 0 and Layout weight of 1. What is the difference when they all have Layout width of 0.25 instead? It seems the output is the same:
Linear layout says, give every textview a(0.25 weight) 25% space (considering 100% available space). since there are 4 textview , so 100% would be divided among 4 as 25% ratio. Now linear layout gives each textview 25% space, textview weight 1 i.e 100% says, fill all the available space that is given to you by linear layout.
Okay so it is fairly simple to understand.
layout_width=0dp and layout_weight=1 means that you want all the views to take equal space horizontally.
whereas
layout_width=0dp and layout_weight=0.25 means that you want all the views to take 1/4 times the space of the total width of the parent.
This value becomes equal in case you have 4 views to be placed and not always.
There is no difference.
The spacing (layout_width) is calculated as a percentage of the total. In other words, if each TextView has a layout_weight of 1 then the total is 4 but each TextView gets 1 out of 4 (i.e., a quarter).
When using 0.25 the total is 1 but 0.25 is a quarter of 1 so, again, each TextView gets a quarter of the space.
I have a fixed number of elements, 3 to be precise, aligned horizontally in a SINGLE row, in my GridView.
Each Grid Element is a Linear layout with vertical orientation.
//Fixed Size NOT OPTIONAL
//Optional text
//optional text
//optional text
//NOT OPTIONAL
As text1/text2/text3 are not compulsory, each GridElement has variable height. If the height of first GRIDELEMENT is less than the other two GRIDELEMENTS, the heights of GridElements two and three are cut as the GridView takes the height of the first element.
I want the GridView to take the height of the tallest element.
Please help me in resolving this issue.
I have 4 TextViews placed horizontally filling all the width of the screen, and each TextView with the same horizontal width . Depending on the device's screen the total width is diferent. I want to change the textsize to fill each TextView horizontally but in a manner that the text fits. Is there any way to do this?? Thx
I think that this link could help you immensly as it is very similar. You may have to SET your widths static based upon you screen width then continue to adjust your font size until the vertical direction fits for each textview.
Calculate text size according to width of text area
I have a linear layout (horizontal) inside which there is 3 linear layouts, each of them have a textview inside them.
Now I wanted to divide these linear layouts equally. So I added weight to them as 1. It works perfect.
But now when the text width inside them are varying they are no more of equal weight :(
Say for any linear layout i removed the text, or wrote a smaller text or varying length text then it is loosing the equal distribution.
What do I do to make the layouts uniformly distributed.
And one more thing i would like to know say the text width increases the max allocated width for these layout (these will be given dynamically may be by doing width = 1), then how do i move the text to next line.
Make sure you set the width to 0dip for each item inside the LinearLayout.