1

I have an EditText field that needs to be restricted on what can go in it.

Only A-Z 0-9 with I and O rejected and a maximum of 17 characters. This is a VIN number field. I have my field setup like this:

 <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="VIN">

            <EditText
                android:id="@+id/txtVIN"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeActionLabel="VIN"
                android:imeOptions="actionDone"
                android:inputType="textNoSuggestions"
                android:maxLength="17"
                android:digits="ABCDEFGHJKLMNPQRSTUVWXYZ0123456789"
                android:maxLines="1" />

        </android.support.design.widget.TextInputLayout>

I would like to add that android:digits="ABCDEFGHJKLMNPQRSTUVWXYZ0123456789" did not work. It was only the input filter that worked.

The below setup is called in onCreateView

    private void setupVIN(View view) {
            final String allowed = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";

            txtVIN = view.findViewById(R.id.txtVIN);
            txtVIN.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS);
            txtVIN.setImeOptions(EditorInfo.IME_ACTION_DONE);

            InputFilter[] vinFilters = new InputFilter[3];
            vinFilters[0] = new InputFilter.LengthFilter(17);
            vinFilters[1] = new InputFilter.AllCaps();
            vinFilters[2] = new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                    if (source != null && !allowed.contains((source))) {
                        return "";
                    }
                    return null;
                }
            };

            txtVIN.setFilters(vinFilters);
            txtVIN.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    //

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    if (txtVIN.getText().length() == 17) {
                        Utilities.hideSoftKeyboard(getActivity());
                        postVINForDecoding(String.valueOf(txtVIN.getText()));
                    }

                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });
            txtVIN.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        if (txtVIN.getText().length() == 17) {
 postVINForDecoding(String.valueOf(txtVIN.getText()));
                        }
                        return true;
                    }
                    return false;
                }
            });
        }

The above code works fine. It will only allow 17 upper-cased characters, excluding I and O.

The problem is when the view is populated with data and is presented,the field is cleared out. I can still type into that field however.

 @Override
    public void onResume() {
        super.onResume();
//test data
        txtVIN.setText("JTJGA31U240037679");

    }

If I remove vinFilters[2] then the pre-populated data shows up.

Any suggestions?

user-44651
  • 3,924
  • 6
  • 41
  • 87
  • Maybe use a RegEx matcher with a TextWatcher that removes the invalid chars – SteelToe Mar 23 '18 at 13:58
  • When you invoke 'setText', the entire string is provided to 0 or more input filters. In your case that means '!allowed.contains(source)' would obviously fail with "JTJGA31U240037679". –  Mar 23 '18 at 16:29
  • Check out the accepted answer at https://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android/4401227#4401227. It's a slight correction to your [2] inputfilter approach where you need to loop on each character provided rather than just assuming it is one character. –  Mar 23 '18 at 16:59

1 Answers1

0

It's a little different from what you've chose to do but I think it should work for you if you just put on your afterTextChanged :

final String result = txtVIN.getText().replaceAll("[^A-Z0-9]", "")
                                      .replace("O", "")
                                      .replace("I", "");

if (!result.equals(txtVIN.getText())) {
   txtVIN.setText(result);
}
RDO
  • 425
  • 4
  • 6