I'm looking for a simple way for Android, when a user taps a in a edit text box, it will add a hyphen inbetween the number set. Sort of like a phone number, but will be set in a custom position.
For example Book number 05, CD number 15, track number5 = 0515-5. So when they start typing, the app will automatically enter the hyphen.
Should I use some type of listener and count how many characters are entered, then when it hits that number of characters, it will add the hyphen?
Thanks!
Use an InputFilter.
See my answers here: press "." many times (validate ip address in EditText while typing) and here: How to set Edittext view allow only two numeric values and two decimal values like ##.## for ideas on how to implement it
Related
This question already has answers here:
What's the best way to limit text length of EditText in Android
(22 answers)
Closed 2 years ago.
I want to prevent the user from inserting more letters than a certain amount to an alert dialog that contain an Edit-text instance. I try to use Input filter. Length Filter, but that only displays the string up to the requested length. The user can still write more characters, and when I try to delete characters for example, nothing happens until I delete enough characters for the word to be shorter than the limit (the like keyboard remembers the keys).
EDIT:
To be more clear, I've already tried using InputFilter. That indeed enforces a string size that is passed to the EditText, but as I said I can still continue to write letters with the keyboard which are not displayed in the text box. When I hit delete, it deletes the extra letters first and only after enough letters have been deleted, I start to see the letters in the text box deleting. My requested scenario: set the string limit to 10. Hit 15 characters in my keyboard. Then hit backspace and see the last letter in the text box get deleted.
I hope this is more clear now.
Can someone help?
Probably it's to late but if someone have the same problem.
In layout:
android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="20"
Programatically:
setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
You can use following propery of EditText in AndroidManifest.xml
android:maxLength="8"
Now user can not add more than 8 characters in the EditText.
You can set this property also at runtime by assigning
youredittext.setMaxLength(8);
You told that you have your edit-text inside alert dialog box so set this at runtime and give a try.
SEE THIS
TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(filterArray);
See this answer for a partial workaround: https://stackoverflow.com/a/19222238/933656
You will be able to immediately delete the last character in the EditText because there is no preview for the keyboard to show (assuming all third-party keyboards respect the textNoSuggestions flag). The only problem is that you lose the suggestion text.
I have an EditText in my app that a user can enter a decimal Volume in.
At the moment i use:
EditText editTextVolume = (EditText)findViewById(R.id.edit_vol);
editTextVolume.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
to get a numerical keypad. Is there anyway I can have this same keypad but with an extra button that is * or x so i can allow the user to input for example 4x200.
I only want that one multiplication character (so no divide or other mathematical operators)
Is this possible?
Not possible without implementing your own custom keypad.
However, if your project requirements are not very strict, you may use InputType.TYPE_CLASS_PHONE. This will allow putting * symbol, but will also make available several other symbols (e.g., #, +, ., ,) used in phone numbers.
I want to enter the text in the format of "X123". i.e first letter is alphatbetical and remaining should be numericals. So is there any input type which fullfill this requirement or I have to use textwatcher for edit text and then change input type of edit text at run time.Is it possible?
Thanks in advance.
There is no standard input type like this.
You can use TextWatcher if you want to force it upon your users while typing, but that might not really be nice neither. You'll only get the effect that they press 'wrong' input and nothing happens.
Perhaps its a much easier, and still nicer way to simply put in a hint which shows the expected input, and once they 'submit' the input you check it against a regex.
If it doesn't match the regex, you can show a Toast, shake the input field, surround the edittext with red lines or whatever you want to show what field has wrong input.
use this RegExp to match the string
string.matches("[a-zA-Z]\\d{3,}") // for compulasory one char and min three digit
string.matches("[a-zA-Z]\\d{2,}") // for compulasory one char and min two digit
string.matches("[a-zA-Z]\\d{1,}") // for compulasory one char and min one digit
string.matches("[a-zA-Z]\\d*") // for compulasory one char and zero or more digit
Hope it will help you.
I am using a TextWatcher to keep track of edittext words. What I would like is to be able to detect when a word is written and then a character is added to the start of the word.
For example, I want to type "hello" and in the moment that "hello" is finished I need to add a "-" prefix, so the final word would be "-hello".
I am not sure if this can be done with a TextWatcher. Can it, and if so, how?
If you want to detect a word then you have to detect that the user has pressed the space button upon this you can simply add the character at the start of the word
Yes you can. Just check whether the user entered the letters matches your predefined specified word list and then simply add the '-'
I am making a math program that reads in numbers from an edittext box, and creates / LU factors a matrix. When a user clicks into this field, the regular keyboard shows. It would be easier for the user if it were a numeric keyboard. When I use android:inputStyle="number" the amount of numbers allowed to be entered into the field is 1. How do I bring up the numeric keyboard without limiting the ability to enter multiple numbers?
Add this in each numeric edittext in your layout file:
android:numeric="decimal"
android:numeric has been depreciated. (From Google: android:numeric If set, specifies that this TextView has a numeric input method. * Deprecated: Use inputType instead.)
android:inputType="number" seems to work great for me. There are a lot of options as well. (From Google: The type of data being placed in a text field, used to help an input method decide how to let the user enter text.)
android:inputType="numberDecimal"