For example textview with size 33.0 dp in xml. It means double number or it has other features?
I assume you're referring to either layout_width or layout_height; in this case these numbers are always floating point. The ".0" on the end has no particular significance.
See http://developer.android.com/reference/android/R.attr.html#layout_width for more information.
Related
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 am trying to fit an EditText into different ImageView templates. For each template, I want to restrict the EditText to a specific width.
For example, in one of my templates, the EditText field should fit a maximum of 300 pixels wide, so I would like to set the text width limit to 300 pixels.
However, I cannot restrict the length by character length because, typing:
"iiiiiii" is not the same width as "wwwwwwwwww."
Would anyone happen to have a solution to this? Thanks.
You could use a textWatcher. This would have a method called onTextChanged(). You could override this to get the length. Once this is done, you could alter the length of the textView itself.
http://www.rqna.net/qna/tssiw-android-edittext-limit-string-length-not-by-characters.html
That guy answered your question with probably the best answer 2 months ago...
You can achieve something similar with android:paddingRight through xml
Is there a way to limit the input length of a Android edit text field using some sort of physical parameters (i.e. inches, pixels, etc.) I do not want to limit the field by character number.
Thanks for the help!
you can use android:maxWidth="100dp" but that is going to set the max width of the View itself, and will not affect how many characters are allowed to be typed into it.
I do not beleive there is an easy way to accomplish what you want. The only thing I can think of is use a TextWatcher and dynamically determine how many characters will fit in the the size that you are wanting (which will be different for different devices). That is still basically "limiting the field by character number" though which you state you don't want to do.
Can you elaborate on why you are wanting to do this? Perhaps there is a better way to solve the overall challenge that you are facing.
You want to add a text changed listener (TextWatcher). In your listener you'll need to measure the size of the font, and the pixel density of the screen, and the number of characters. Multiply all of those together and you should know how physically long the text is (with a few caveats).
Having said that, this seems like a weird restriction and I'm not sure why you want this. Every device is going to have a different screen size, so measuring your interface in inches is typically a bad idea.
You could find how how many M's will fit in the printed form and then use EditText.SetMaxEms To make sure the input will be printable on the form.
Try this on your EditText
android:maxLength="10"
I have TextView with height and width as fill parent. Is it possible to find out how many characters can this layout hold?
Do you mean how many characters can be entered into the textview and still be fully visible without scrolling? For proportional fonts, that will depend on the specific characters typed, including where the line break opportunities are. I don't think there's a simple way to compute that.
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