0

I am working with a EditField in my application, to enter phone number to DIAL.

Now My query is,

1) if begins with 1, it should be only 10 max digits after the 1, 

2) if 011, up to 15 digits max, but no fewer than 8 after 011.

Please let me know, how this can be done in EditField.

Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67
  • 1
    check this link http://marakana.com/forums/android/learning_android_book/487.html for applying textwatcher on your edittext may be this will help you. Rest logic you have to write on your own – Abhinav Singh Maurya Feb 22 '13 at 10:24

2 Answers2

1

add textWatcher to your edittext and change the max limit

Kapil Vats
  • 5,485
  • 1
  • 27
  • 30
0

Here is answer to your solution. Its a very poor solution just a hint type but you can change code as according to you

final int LIMIT_FIRST = 10;
        final int LIMIT_SECOND = 15;
        final EditText et = (EditText) findViewById(R.id.edittext);
        et.setSingleLine();
        et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String stringValue = s.toString().trim();
                int stringLength = stringValue.length();
                if(stringLength == 0){
                    InputFilter[] FilterArray = new InputFilter[1];
                    FilterArray[0] = new InputFilter.LengthFilter(1000);
                    et.setFilters(FilterArray);
                }else if(stringLength == 1){
                    if(stringValue.equalsIgnoreCase("1")){
                        InputFilter[] FilterArray = new InputFilter[1];
                        FilterArray[0] = new InputFilter.LengthFilter(LIMIT_FIRST);
                        et.setFilters(FilterArray);
                    }
                }else if(stringLength == 3){
                    if(stringValue.equalsIgnoreCase("011")){
                        InputFilter[] FilterArray = new InputFilter[1];
                        FilterArray[0] = new InputFilter.LengthFilter(LIMIT_SECOND);
                        et.setFilters(FilterArray);
                    }
                }
                System.out.println(s);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                System.out.println(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                System.out.println(s);
            }
        });

Let me know if this helps you

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51