Multiple input filter android editText

254 Views Asked by At

I am applying input filter on edittext with below conditions

  1. The input of edit text should accept numberDecimal which is defined in xml (working)
  2. first digit of input should not be "." (dot) like ".9" (working)
  3. edit text can have "." after first digit like "1.9" (working)
  4. maximum length is 6 (not working)

but in this case only .(dot) filter is working, not maximum length. Please help how to make it work. I tried reverse filter setting like first dot and then maximum Length, but nothing works!

 edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6), ignoreFirstDot()});

 // ignore first input as dot on edit text
 public static InputFilter ignoreFirstDot() {
    return 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 (source.toString().startsWith(".")) {
                    if (dstart == 0)
                        return "";
                }
            }
            return null;
        }
    };
}

Here is my Edit Text from XML. even though defined in xml as maxLength =6, but still not working

                    <EditText
                    android:id="@+id/edt"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/margin45"
                    android:layout_weight=".5"
                    android:background="@drawable/edit_border_square"
                    android:inputType="numberDecimal"
                    android:lines="1"
                    android:maxLength="6"
                    android:maxLines="1"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:textColor="@color/black"
                    android:textCursorDrawable="@drawable/color_cursor"
                    android:textSize="@dimen/text11" />

    
0

There are 0 best solutions below