1

I have an decimal signed EditText (so it's impossible to write a comma). I put a TextWatcher on it but it only listen chars allowed to be written.

However I want to catch when the user tries to write a comma to show a toast.

How is it possible to do that?

Thank you

3 Answers3

1

You could attach a TextWatcher to the EditText (like you said), but then you could implement an if/else statement to check the values in the EditText. Here is an example of what you could do: editText.addTextChangedListener(new TextWatcher() {

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

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(String.valueOf(s).contains(",")) {
            String editTextContent = String.valueOf(editText.getText());
            editText.setText(editTextContent.replace(",", ""));
            Toast.makeText(getApplicationContext(), "Enter a valid input", Toast.LENGTH_SHORT).show();
        } else {
            // User entered something valid
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

This will make it so that the user cannot enter a comma. Hope it helps!

AkashBhave
  • 749
  • 4
  • 12
  • Thank you for your fast answer but do you think it will work? Because, as I said, I put restrictions (decimal and signed number) and just to test I put a toast in the beforeTextChanged and only the numbers and the points were called. – Herel Adrastel May 20 '16 at 21:35
  • Have you tested this? I think this wouldn't work, as already mentioned in Herel's post, he won't be able to type in the comma. So checking with a `TextWatcher` doesn't do anything. – AL. May 20 '16 at 21:36
  • Isn't that the goal? @Herel wants to catch when the user types in the comma. The TextWatcher watches for the the user types the comma. – AkashBhave May 20 '16 at 21:39
  • The `TextWatcher` will detect changes for the value inside the `EditText`. Not if its typed. Since the comma is already restricted. If he types it in, the text value wouldn't change, and the `TextWatcher` will end up listening for something that can never happen. – AL. May 22 '16 at 00:16
  • I believe I misunderstood his question then. I thought he wanted to have commas with a `Toast` being displayed when it's typed. – AkashBhave May 22 '16 at 01:37
1

So I tried out the suggestion I made in the comments where you make use of setOnEditorActionListener() and instead of using the actionId, I used the KeyEvent event and checked it's value if it's equal to KeyCode.KEYCODE_COMMA. Unfortunately, it didn't work. Searched around the community and a lot of other site on how to do it, but the closest I can manage to do is to detect the KeyEvent.ACTION_DOWN.

I think since the setting of your EditText is to already restrict the comma to be entered, the system itself is also ignoring it. One thing I don't get though is to why catch it if the user has no way of doing it?

What I suggest for this is to make use of hint (if what I think of your use case is correct) to inform the user on what is supposed to be entered for that specific EditText. Or you could try to create your own softkeyboard. :D

If you do, however, managed to find a workaround for this, do tell. I'm very much interested. :)

Cheers! :D

AL.
  • 36,815
  • 10
  • 142
  • 281
1

try this specify your character in your edit text like this may it will help you android:digits="abcde.....012345789"

  • good answer. What ever the digits you wanna write in EditText you can. Except those user can not enter any other digits. – Tara May 25 '17 at 07:45