I have set input type to be numberdecimal but also want to populate the editText with a "-" programmatically. I can add the text but then I am unable to edit the text as it doesn't confirm to the number decimal format. Any idea on how I can say inputtype is numberdecimal but a "-" can be allowed?
6 Answers
I was able to achieve this behavior by setting digits xml attribute as follows:
<EditText
    ...
    android:inputType="number"
    android:digits="0123456789-"/>
Setting it up programatically (Set EditText Digits Programmatically):
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789-"));
I managed to do that with:
android:inputType="number|numberSigned"    
android:digits="0123456789-"
- 1,241
 - 16
 - 16
 
I found a very easy solution:
editText.setKeyListener(new DigitsKeyListener(true, true));    
The first true is for whether is signed input, the second true is for decimal.
Hope that helps
- 27,194
 - 17
 - 102
 - 148
 
- 
                    Gustavo, bemace, sorry for not being clear. I am talking about the hyphen inbetween numbers. something like 1-800-555555 and not the sign at front of the number. – bschandramohan Jul 19 '11 at 16:27
 
You will have to write your own KeyListener. You could start by downloading the source of the NumberKeyListener and take it from there.
Martin
- 11,577
 - 16
 - 80
 - 110
 
- 
                    Martin, is there a way I can look at the source code without downloading it from the github? – bschandramohan Aug 05 '10 at 05:55
 - 
                    1Well there is the web interface at http://android.git.kernel.org/ - but it does not look very helpful. If at all possible you should try to get the source. – Martin Aug 05 '10 at 07:55
 - 
                    1This answer might help you: http://stackoverflow.com/questions/4172242/live-editing-of-users-input/4172392#4172392 – aleung Feb 19 '11 at 16:40
 
I am having one solution, which may helps you:
Suppose, You want to enter 2-3 numbers with "-" sign for e.g. 203-304-405.23-232.45,
then Allow user to enter this in EditText without setting any attributes.
and then you can Separate each numbers with "split()" function , but be sure that there should be any separator sign in between the tokens.
then
 String tokens[];
 strInput = edittext1.getText.toString();
 tokens = strInput.split(",");
then you can work with each tokens separately as tokens[0], tokens[1], for example:
num1 = tokens[0];
num2 = tokens[1];
Hope this helps you.
Enjoy!!
- 127,700
 - 71
 - 241
 - 295
 
- 
                    Thanks Paresh for your insights... You are talking about post processing of the text entered by the user but unfortunately I cannot allow user to enter anything in EditText other than numbers. InputType has to be fixed to decimal :( – bschandramohan Aug 04 '10 at 05:47
 
In my case, I just created a simple class that allows numbers and hyphen only.
private static class myFilter implements InputFilter {
    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        // Allow digits and hyphen in the input
        String filters = "0123456789-";
        StringBuilder filteredStringBuilder = new StringBuilder();
        for (int i = start; i < end; i++) {
            char currentChar = source.charAt(i);
            if (filters.contains(String.valueOf(currentChar))) {
                filteredStringBuilder.append(currentChar);
            }
        }
        return filteredStringBuilder.toString();
    }
}
Then in the ActivityClass, to add filter to the EditText:
    EditText _eText;
    _eText = findViewById(R.id.editText);
    _eText.setInputType(InputType.TYPE_CLASS_TEXT);
    _eText.setFilters(new InputFilter[]{new myFilter()});
- 443
 - 1
 - 6
 - 17