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);
Related
Problem : While adding text to a text view in android , when text is long enough to make textview height greater than screen, the textview is only increasing its height up to the screen height and not going beyond the screen.
Requirement: To increase the height of text view according to the text content .
Is it possible to change the height of a text view at runtime?
EDIT
Even though I change the height of TV (greater than height of screen) in XML
still my TV height is not exceeding screen height.
EDIT 2
I achieved this by drawing the TextView using canvas. I am not sure how efficient this is.
I used this code:
Rect bounds=new Rect();
tv.getPaint().getTextBounds(text,0, text.length(),bounds);
float textWidth=bounds.width();
float textHeight=bounds.height();
and it works well horizontally...
also vertical size is ok...
but the text is painted in a position lower than what I expect...
these links was not useful:
get text height
auto scale textview text to fit within bounds
what should I change?
view and its text
Just set your layout params to WRAP_CONTENT for the height. If your text is in a LinearLayout it would look like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myTextView.setLayoutParams(params);
If you're using a XML layout just set your TextView's layout_height to wrap_content. If the text is still painted lower than you expect check whats the padding set to the TextView.
In my chat program, in order to show the length of the voice, I use a custom LinearLayout to display for users, it has two TextView ,one is to show how much time the voice has, the other is to show how long the voice is,
For example: a 10s length voice's TextView width is 20dp, a 5s length voice's TextView width is 10dp.
How can I fix it ?
You can try this way:
textView.getLayoutParams().height = 50;
textView.getLayoutParams().width = 100;
Or if you want to assign values in dp than you can specify height and width values in dimen file and can do it this way:
textView.getLayoutParams().height = (int)getResources().getDimension(R.dimen.textview_height);
textView.getLayoutParams().width = (int)getResources().getDimension(R.dimen.textview_width);
I got a horizontally oriented LinearLayout with an ImageView and a TextView. TextView has a text of 1- or 2-digits number.
What I need is to set text size of a TextView to be exactly same in a hight of an ImageView's image.
I know I can get ImageView's Image height , but how can I make textSize as high accordingly?
View.getHeight():
Return the height of your view. Returns
Returns
The height of your view, in pixels.
TextView.setTextSize(float):
Set the default text size to the given value, interpreted as "scaled
pixel" units. This size is adjusted based on the current density and
user font size preference.
Parameters
size The scaled pixel size.
This basically means you need to convert the height (px) to text height (sp):
float density = getResources().getDisplayMetrics().scaledDensity;
float textHeight = imageHeight / density;
Give constant height-width to both(image view and textview)... or try scaleType="fitxy"...
You can try:
float height = imageView.getHeight();
TextView tv = new TextView(context);
tv.setTextSize(height)
I'm not sure this will do exactly what you want, but it is the most obvious starting point I see.
well, I've found myself the following :
first we need to find a real height of an ImageView's Image - it's not as high as the ImageView, so it's explained well here : How to get the width and height of an android.widget.ImageView?
next we set the height of the text :
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
imageView.getMeasuredHeight()*(imageView.getMeasuredHeight()/
imageView.getMeasuredWidth()));
which means we are setting textSize in raw pixels, then we take height and multiply it with the picture's ratio of height to width so TextView's text size will be exactly high as the image
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