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"
Related
I am writing an app about eye test. It is necessary to set the standard text size. I used the following code but it showed what I did not expect.
Typeface type=Typeface.createFromAsset(getAssets(),"Optotypes.ttf");
textView2.setTypeface(type);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_MM,25);
textView2.setText(randomLetter);
I expected the textview show a 2.5cm letter but it is not the exact length/height still.
This situation appear also on different device.
The next problem is that the size is different between the original font and ttf I added. (the original font didn't show the text with 2.5cm also.
Is my code wrong or anything else i missed ? Thanks guys . it is important to me.
I think you're missing how Android handles text sizes.
In Android, you should specify text size in SP units, so Android can scale it accordingly to the user's font size preferences. Never specify hardcoded pixels or centimeters.
Check this references for documentation on the subject:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
http://developer.android.com/reference/android/widget/TextView.html#attr_android:textSize
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you want to set the text size in SP programatically, you can do this
// same as android:textSize="15sp" in XML
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
-- EDIT
Keep in mind that by just setting a certain text size it doesn't mean that every letter will be of that size. Remember that there are multiple letters with multiple sizes. For instance, with a size of 20mm, this is what you get
Because Android needs to accommodate every possible character in a textview with the size you provided. That being said, textSize is not 100% accurate to what you provide to it.
If this is not enough for you, please provide more details of the problem you have at hands.
Is it possible to develop something like this in Android?:
Note that the length of any of the three text components is dynamic.
I pretend to receive a first String (that might be multilined) then an input field for the user, and finally, another String (can also be multilined). I want them all to seem like they belong to the same sentence.
I'm not asking for the full code, just some clues.
Thanks in advance.
I finally achieve it by using FrameLayouts and with the help of this posts:
Android: measureText() Return Pixels Based on Scaled Pixels
Get current visible text in textview
How to find android TextView number of characters per line?
I have a huge string which I have to truncate at one point, As I have to run the code in different devices with different screen sizes, problem arises with no. of lines per view in different devices. for this, obviously I should not use any methods like setMaxLength() but whereever in the the Internet I see the method every one is prescribing to set no. of lines, which is not suitable for my case.
Please some one prescribe me a procedure to truncate the text dynamically but not according to the number of lines.
If you want to truncate and not wrap the text, you could consider using
setEllipsize.
I am trying to have a custom EditText based on the background that i am using for. The Background image has some spaces between the entry areas so i need to have some space between the characters(kerning) to fit them right in. So for example after every character the user enters, i need to put 4 whitespace after that.
I couldn't find any solution for this on the net so far, some people suggested TextWatcher, but i couldn't manage to make it work as i want it too.
Can someone help me about it?
Thanks
I have you considered using a custom font? Some font types are made to stretch out or shrink or have empty spaces. With so many different fonts available online, you can definitely find something. You can also make your own with a software. It might be time consuming if you start the lettering from scratch. I'm not 100% sure if it'll fit exactly to your background, but it's idea that you can consider.
If it doesn't fit, I supposed you can always customized the background to fix your font too. Here's the code for those who might want to use custom fonts in their app too.
Typeface myfont = Typeface.createFromAsset(getAssets(),
"fonts/Blocks2.ttf");
myeditText.setTypeface(myfont);
The font is in the asset folder under another folder called fonts.
This question is related to How to change letter spacing in a Textview?
As shown at this issue: android format edittext to display spaces after every 4 characters a solution might be to insert spaces with Java code...
You need to use TextWatcher to achieve visual purpose spaces.
And use any simply split string by space logic to join it back or loop
through the entire string per character wise and eliminate (char) 32
from the string
As far as i know actual character spacing is not possible, though i'd love to use that myself as well.
Another option might be to use a custom font with the character spacing included.
How can I determine the number of visible characters that a TextView can display. For example if I change the orientation this number may change. If I change the resolution then also the number of visible characters changes.
Thanks in advance
Thank you for your answer.
Currently I am developing a small text based game to become acquainted with the Android API. For that reason I need to know exactly how much characters can be displayed in the visible area of a TextView widget. I saw an example of Paint but wanted to know if there are better solutions.
Ideally, you design your GUI such that it does not matter. For example, you can use android:ellipsize to deal with strings that are too long for the available space.
There are classes in the 2D drawing APIs (e.g., Paint) that seem to be tied into this, but it does not look like much fun.
You can use ellipsize property but there has been a bug that has been filed on the same
http://code.google.com/p/android/issues/detail?id=2254
On the bottom of this page you could find an alternate approach which can draw exactly the number of lines on a given space...