June 24, 2011

Android Number Input Field Validation

Input Field Validation:

code:

editText.setOnFocusChangeListener(new OnFocusChangeListener(){
                @Override
                public void onFocusChange(View v,boolean hasFocus)
                {
                    //check for emptiness
                    if(editText.length()==0)
                    {
                        editText.setError(error_required);
                    }
                    else if(!numberValidation(editText.getText().toString()))
                    {
                        editText.setError("Only Numbers");
                    }
                }
            }
            );

In main.xml add,

EditText tag, android:numeric="integer/number/decimal"

References:
http://phaniraghav.wordpress.com/2011/05/07/android-edittext-validations-2/
http://stackoverflow.com/questions/5218691/display-input-validation-errors-in-popup

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com