Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Measure TextView Height Based on Device Width and Font Size?

Tags:

I am looking for method in Android that will take a input (text, text_font_size, device_width) and based on these calculations it will return how much height will required to display particular text ?

I am setting a text view/ webview height runtime based on his content,I am aware about warp content but I can't use in my case because of some web view minimum height issue.

So I am trying to calculate height and based on that I am setting view height.

I have tried with following ways

Paint paint = new Paint(); paint.setTextSize(text.length());  Rect bounds = new Rect(); paint.getTextBounds(text, 0, 1, bounds); mTextViewHeight= bounds.height(); 

So output is

1) "Hello World" returns a height of 13 for font 15

2) "The latest version of Jelly Bean is here, with performance optimizations" returns a height 16 for font 15

Then I have tried with

Paint paint = new Paint(); paint.setTextSize(15); paint.setTypeface(Typeface.SANS_SERIF); paint.setColor(Color.BLACK);  Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), result);  Paint.FontMetrics metrics = brush.getFontMetrics(); int totalHeight = (int) (metrics.descent - metrics.ascent + metrics.leading); 

So output is

1) "Hello World" returns a height of 17 for font 15

2) "The latest version of Jelly Bean is here, with performance optimisations" returns a height of 17 for font 15

if I set these values to my view then its cutting some text, its not showing all content.

Again it looks OK on some tables as its having big width but not on phone.

Is there any way to calculate height based on content ?

like image 437
Mac Avatar asked Jan 11 '13 11:01

Mac


People also ask

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

Which method is used to change the text size of the TextView?

To use preset sizes to set up the autosizing of TextView programmatically, call the setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) method. Provide an array of sizes and any TypedValue dimension unit for the size.

Which attribute increase or decrease the font size of TextView?

app:autoSizeMinTextSize=”10sp” using this attribute the TextView will be resized up to the size of 10sp and app:autoSizeStepGranularity=”2sp” using this attribute we are uniformly reducing the size of the TextView as 2sp when it goes out of the screen.


2 Answers

public static int getHeight(Context context, String text, int textSize, int deviceWidth) {     TextView textView = new TextView(context);     textView.setText(text);     textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);     int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST);     int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);     textView.measure(widthMeasureSpec, heightMeasureSpec);     return textView.getMeasuredHeight(); } 

If textSize is not given in pixels, change the first paramter of setTextSize().

like image 138
Matthias Robbers Avatar answered Oct 24 '22 11:10

Matthias Robbers


I've got a easier method to know the real height of a line before be painted, I don't know if this help you guys, but my solution to get the height of a line it's independent of the height of the layout, just take the font metrics like this:

myTextView.getPaint().getFontMetrics().bottom - myTextView.getPaint().getFontMetrics().top) 

with that we get the real height that the fonts will take from the textview to be painted. This not give you an int, but you can make a Math.round to get a near value.

like image 32
jfcogato Avatar answered Oct 24 '22 10:10

jfcogato