I have a seemingly simple task in Android, which turned out to be quite complex.
I have multiple strings, and I want each string to be centered based on a character in a specific position in that string. For example "azcde" should be centered on a character in index 1 ("z" in this case), "trxdsf" should be centered on a character in index 2 ("x" in this case), ...
Below is an example.
The problem is that characters vary in size (e.g. "i" is much thinner than "m")...
Can this be done in TextView or custom View?
Related
I need to show the price in the Android app. Design is such that text size of two digits after the decimal point is smaller than the rest of the text. I know I can achieve this with multiple TextView views but I'm wondering if anyone did it without the use of multiple views and how to do it.
TL;DR Price should be formatted like this: 5,oo USD (oo in this example represents two zeros as in zero cents with text size smaller than the number 5 which represents the number of dollars)
You can use AbsoluteSizeSpan for your TextView
SpannableStringBuilder yourText = new SpannableStringBuilder("5.00 USD");
span.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(span)
You can find more information from developer webpage
You can
Check the font face that it zeros are smaller that the rest of the digit and apply it.
I have a TextView that displays 13 labels by using '\n'. I chose to use only one View, since android-layouting tends to get very slow if you have more than a couple of TextViews.
I set line-spacing-multiplier to 1.3x and have the problem that between the first and the second line there's about 5 to 10 px more space than between the others. There are no special characters like Ö in the labels, everyone of them consists of capital letters only.
Is this a known bug?
I have a question about one android text rendering detail:
Whether current character rendered width depends from previos character in android text renderer (for example in textview)?
For example if char 'x' have width equals x_w and 'y' - y_w, will string "xy" always have with equals
x_w + y_w
?
Some more details:
The point is that I should measure strings as quiсk as possible. I want to measure once each letter (character) from text using Paint.getTextWidths(...) and then jsut use work with characters width.
Depends on the font. It's not something I would assume, if you want the width of a string, measure the full string.
If you absolutely need that, use a monospace font.
I have text that may be ellipsized in two lines. But no matter how many lines it takes, I need that some additional (short but length may vary) text would ALWAYS be fully visible at the end of previous text.
Here are three cases representing my problem:
How can I achieve this? I tried measuring text length and splitting it into two TextViews but it's not accurate enough and also hits performance badly.
In my app I am displaying a table where the individual cells are editable text fields (i.e. EditText objects). I am currently struggling calculating appropriate widths for these.
If I set the width to be as many "Ems" as the displayed text has characters, my fields are much to wide by about a factor of 2 and the table looks ugly and wastes lots of precious screen space. Setting the width in "Ems" is essentially like assuming the worst case, i.e. the width of a string under the assumption that it contains only the broadest characters in a font, usually 'M' (hence the name of the method), 'm', 'W', or '_'). But on average strings contain narrower characters and so most of these fields are half empty and much too wide for their actual content. I am thus desperately seeking a way to calculate a better fitting width, not that worst case width.
The "normal" way to do this in Java (in AWT or Swing at least) is to asks a widget's current Font (actually a Font's FontMetrics), to calculate and return the width that will be necessary to display a given string in pixels. But how does one obtain a TextView's Font? I haven't found any method to obtain a (Text)View's font and/or calculate a more appropriate width given the actual content of a cell's text string. How does one do that in Android?
Michael
I just found the "missing link": <TextView>.getPaint() is the answer!
The "Paint" then has methods like <Paint>.measureText(...) to calculate a string's width and <Paint>.ascent() and <Paint>.descent() to calculate a string's height.
Michael