1

This is my edittext

        <EditText
        android:id="@+id/etWaardeVan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:digits="0123456789."
        android:inputType="number|numberDecimal"
        android:scrollHorizontally="true"
        android:singleLine="true"
        />

How do I programmatically allow for only one dot to be input.

Zoe
  • 27,060
  • 21
  • 118
  • 148

6 Answers6

1

Is this what you are looking for?

Set EditText Digits Programmatically

etWarDevan.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
CodeBulls Inc.
  • 462
  • 3
  • 11
1

I'd remove the last entered character if its a dot and there has been one before.

EditText field = view.findViewById(R.id.etWaardeVan);
field.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                String str = editable.toString();
                String strold = str.substring(0, str.length() - 2);
                String lastchar = str.substring(str.length() - 1);
                if(strold.contains(".") && lastchar.equals(".")) field.setText(str);
            }
        });

Hint: this only works if the user doesn't jump back and enters the dot in the middle of the string. You may want to disable cursor movement. (Like this or using android:cursorVisible)


Alternatively (if the input always contains a dot) you can just create two EditTexts without a dot allowed.

jemand771
  • 457
  • 1
  • 6
  • 17
0

Here is my solution, and it works:

dot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //only show one dot if user put more than one dot
            if(input.contains(".")){
             //then save what it has
                input = textView.getText().toString();
            }else {
            //concat the input value
                input=input+".";
            }
        }
    });
Lulu Chen
  • 11
  • 1
0

Basically, EditText is derived from TextView which has a

void addTextChangedListener(TextWatcher watcher)

method. TextWatcher has callbacks, like

abstract void afterTextChanged(Editable s)

that you can implement in order to filter the new text to only contain period.

Regex: (?<=^| )\d+(\.\d+)?(?=$| )

Example

 et1.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
            // Do something here with regex pattern
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
0

In AfterTextChangeListener add Regex

val isSingleDot = Regex("\.").findAll("sample.sample").count() <= 1

-2

Set inputType attribute in EditText/TextInputEditText as android:inputType="numberDecimal" 

KoOPa
  • 1
  • 1