Calculate ems value according to the string length count - android

I want to calculate the ems value according to the string or text length count If text length increase it should occupy it's width size according to the characters count without cut-off text in a single line, thanks
Tried to keep string length as ems but it gives very long width size with empty spaces, Thanks in advance

Using monospaced fonts you could count the em-width of a string. Since letters like i and m do in other cases not have the same length, the pixel/em/dp count cannot be predicted.
If someone would write a sophisticated logic for the widths of letters, count them, then multiply count with per-letter-length, this solution would still be too inaccurate without even more refinements: Ligatures and kerning make the actual width of a word even more complex. For example, A and V can overlap a little, while a capital A following another A or the letter V following another V are same width at at base or top, respectively. Therefore their centers have to be moved away from each other so as to not overlap.

Related

How can I get the actual text size in px(not textSize attribute)?

My question is not about the number of chars nor the textSize attribute in TextView/EditText.
I have a fixed EditText. And the maxLength is 10. And all the chars must be shown at once.
However, each char has different width and height. For example, l and L. If you drag the mouse over(block selection) the l and then the L. You will notice that they have different width.
So, LLLLLLLLLL and llllllllll has different width.
For instance,
LLLLLLLLLL
llllllllll
(both are 10 letters)
Moreover, different font shows different width.
My question is how to get the actual width of chars in PX so that the string won't hidden in the EditText. Let's say, the width of EditText is 200px. And it's limited to 10 letters. And it will show
LLLL(LLLLLL) // (LLLLL) is not shown because of the EditText's width. But you can see when you move the cursor.
llllllllll
How can you get the width of the chars?
you can use TextPaint and measure letters, but there is an easier way for your case. get familiar with em unit
you can declare this value for EditText/TextView by setEms(intNum) or android:ems="intNum". set also width to wrap_content and your View will have width of intNum widest characters (usually Ms). there are also useful minEms and maxEms attributes
edit: HERE you have more complex explanation
PS. don't declare sizes of your Views with px unit due to different densities, use dp. some units explanation in HERE

Can I know how many characters can fit into TextView of X dp width?

Is there a way to know how many characters of font size 10sp can fit into a TextView of a fixed width (let's say 100dp)?
I need to track if the whole string will fit (be visible) into a TextView of predefined width.
I haven't been able to find a way to track or calculate this.
Since Android uses proportional fonts, each character occupies a different width. Also if you take kerning into account the width of a string may be shorter than the sum of the widths of individual characters.
So it's easiest to measure the whole string by adding one character at a time until (a) the entire string if found to fit within the limit, or (b) the width of the string exceeds the limit.
The following code shows how to find how many characters of size 11px fits into TextView 100px wide. (You can find formulas to convert between px & dp on the Web).
We'll do it by using measureText(String text, int start, int end) in a loop incrementing the value of end until it it no longer fits the TextView's width.
String text = "This is my string";
int textViewWidth = 100;
int numChars;
Paint paint = textView.getPaint();
for (numChars = 1; numChars <= text.length; ++numChars) {
if (paint.measureText(text, 0, numChars) > textViewWidth) {
break;
}
}
Log.d("tag", "Number of characters that fit = " + (numChars - 1));
If performance is poor, you may try using a binary search method instead of a linear loop.
The best way to determine this would just be to test it. Since you are using DP it will be relatively device-independent. Unless you are using a fixed-width font there isn't really a way to determine it theoretically unless you want to actually try and measure the width of each letter, the kerning, etc. and compare to the width of the TextView

Android TextView measureText for Arabic

Paint.measureText is not accurate with Arabic text. Here is what I tried:
I have a TextView tv1.
tv1.getWidth(); gives 480, which is okay. tv1.getPaint().measureText("المؤلف : عبدالرحمن ابن أبي بكر أبو الفضل السيوطي"); gives 502, which is bigger than the screen width.
Bad result, since the text is taking about 80% of the line width! The result should be around 400.
Thank you.
i faced this problem today . everything is working as expected and you should know that when your text contains Arabic vowels like ّ ِ ُ َ chars , they are all being calculated in the width as seperate chars and android Paint.measureText is not aware that it shouldnt take those chars into account , because they wont add extra width and they will be drawn on top and bottom of other chars.
the solution is : you should first remove those chars from text and then measure your text.

Android. Calculate the number of characters that fit on the screen

I am writing a reader under Android. Pages are separated into different View. How to calculate how many characters fit on the screen to correctly divide the text into different View? Maybe there is a simple method to do this?
The code for adding text to Views:
for (int i = 0; i < pages; i++) {
TextView textView = new TextView(getApplicationContext());
textView.setText(text);
realViewSwitcher.addView(textView);
}
you could probably make assumptions based on text size, maybe the default here would be 12dip. And then assuming that the average word length is 5 words. determining how many dips a 5 character words takes. and then taking the width and height and and divide it by that number.
Maybe Paint can help you. There is methods like "measureText", or "breakText". You can get a Paint object from a TextView with getPaint().
perhaps you could calculate the number of ems that fit in the textview somehow.
or perhaps you could create an algorithm that try's to fit the characters based on computerVerticalScrollRange(). Through trial and error, it could eventually find the perfect fit.

Android: How do I know how many text can fit in my textView (my page)?

I want to create a text reader, which will cut the text and put it in the TextView (represents my pages) and one after the other. These are contained in a ViewFlipper (represents my entire text), which allows me to turn the pages.
By cons I can not cut my text intelligently. For now I limit each TextView to a number of characters, but it is very unstable. Indeed some letters take up more space than others .. We can not use this method.
How can we know what quantity of text will enter into a TextView?
I tried to find the maximum size of my View manually .
I invoked in:
float test = view.getPaint().measureText(myString);
It works well. When I put 480 letters (0 and 1), I get to 14,400 and my text is entirely within the screen. With:
1 -> 12.0
0 -> 12.0
é -> 11.0
\n -> 13.0
\r -> 7.0
total to fill the page -> 14400
The problem is that the line breaks and carriage return are causing problems.
If I have only letters or numbers in my string, so good: if we respect the maximum size of 14,400, everything is displayed on the screen. No Problem!
By cons, if a line breaks or a carriage return, it does not work. Even if we we respect the limit of 14400, the text above and is not fully displayed.
This is because the "MeasureText" method;" only returns 13 for a line break. But it's weird, a line break should account for at least 480 to make it consistent.
How do publishers eBook (Moon Reader, + Aldiko) for the text to be formatted so good? Each page has the same height, the text ends in the same place ..
You can fix the number of lines in your TextView by adding android:maxLines="some_integer". The other way is this:
if (myTextView.getMeasuredWidth() < myTextView.getPaint().measureText(myText)) {
myTextView.setHorizontalFadingEdgeEnabled(true);
myTextView.setHorizontallyScrolling(true);
}
But this will add cloudy effect to the last character if the text is too long.
Before get measured width or height call below method :
anyview.measure(0,0);
without calling this method you all wage getting 0 value for height and width

Categories

Resources