I am measuring a string text to see if I should add a '\n' for wrapping.
I am using the Paint.measureText function, and it works most of the time but it occurs to me that this isn't accurate since a px doesn't equal a dp - I've seen online how to convert pixels to dp, but instead what I would rather do is convert px to a percentage of the screen size for example
if 'a' is 8 pixels wide, how would I say
float LetterWidthPercent = _______ //get width of character in percent of screen width
float LetterHeightPercent = _______ //get height of character in percent of screen height
This way I could just see
if (LineOfTextWidth >= ScreenWidth * 0.9f) //90%
This would be an extremely useful function to have handy.
You will need to get the screen width through DisplayMetrics or how ever...
For getting the character size use paint with bounds to calculate.
characterWidth/screenWidth = characterScreenPercentage
I made it a double but you can easily change it to a float.
For Characters:
For strings:
You can use following methods to convert do to pixel and pixel to dp.
Convert dp to pixel:
Convert pixel to dp:
You can measure a TextView with text inside before(or instead) drawing it on a screen. At this way you can set any font or textSize for TextView and always know how many percents the text will have on the screen.