Search code examples
androidkeyboardandroid-edittextnumeric

EditText setInputType(InputType.TYPE_CLASS_NUMBER); don't work


I am try using create one editText that only receive numeric (only appears numeric keyboard). On nexus 7 that don't happen :X

this is my code:

    EditText edit = new EditText(context);
    edit.setText(value);
    edit.setTextSize(16);
    edit.setTextColor(getResources().getColor(R.color.blue));
    edit.setInputType(InputType.TYPE_CLASS_NUMBER
            | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    edit.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            elem.setValue(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
    });

Can you help me?

EDIT:

i am add this EditText to a LinearLayout like this:

    myLinearLayout.addView(edit);

Solution

  • try this it will help you

    change, instead of this line

    edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    

    change like below

    edit.setRawInputType(InputType.TYPE_CLASS_NUMBER |InputType.TYPE_NUMBER_FLAG_DECIMAL);