1

I need a edit text validation which will only allow number and : symbol.and length should not be more than 5 char.And most I am creating this edittext pragmatically

    final EditText timeVisited = new EditText(this);
    timeVisited.setId(20);
    timeVisited.setText(Tm);
    timeVisited.setTextColor(Color.BLACK);
    timeVisited.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
    timeVisited.setHint("eg. 14:30");
    timeVisited.setPadding(5,5,5,5);
    timeVisited.setSingleLine(false);
    timeVisited.setLines(1);
    timeVisited.setBackgroundResource(R.drawable.text_bg);
    customerToCatchTblRow.addView(timeVisited);// add the column to the table row here
    LinearLayout.LayoutParams params5 = (LinearLayout.LayoutParams)timeVisited.getLayoutParams();
    params5.setMargins(0, 0, 5, 0); //substitute parameters for left, top, right, bottom
    timeVisited.setLayoutParams(params5);

4 Answers 4

1

Use android:digits attribute for filtering symbols:

<EditText
    android:digits="0123456789:"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> 

or programmatically:

 editText.setKeyListener(DigitsKeyListener.getInstance("0123456789:"));
1

Try this, it programmatically sets the digits allowed for the EditText for validation purpose, the alphabets/numbers/symbols can be mentioned in which the user can enter only those characters

timeVisited.setKeyListener(DigitsKeyListener.getInstance("0123456789:"));

For setting maxlength via code,

int maxLength = 5;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
timeVisited.setFilters(FilterArray);

Let me know if that worked.

1
  • It's opening a number keypad which don't have the sign
    – A J
    May 26, 2013 at 13:43
1

use InputFilter. Code can be something like this

    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (!Character.isDigit(source.charAt(i))
                        && source.charAt(i) != ':') {
                    return "";
                }
            }
            return null;
        }
    };

    timeVisited.setFilters(new InputFilter[] { filter });

And to limit the lenght in xml of edittext use

android:maxLength="5"
2
  • It says The type new InputFilter(){} must implement the inherited abstract method InputFilter.filter(CharSequence, int, int, Spanned, int, int) at InputFilter filter = new InputFilter() {
    – A J
    May 26, 2013 at 13:45
  • may be you are using this in wrong place.. use the code in oncreate for testing.
    – stinepike
    May 26, 2013 at 15:09
0

Why are you using EditText for getting time, Use TimePicker instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.