1

I am using a tag system similar to SO, and instead of forcing the user to find the dash in their keyboard I want space to automatically be translated to a dash.

I am trying to implement it with this textWatcher, but the app doesn't let me type the space bar (it kinda flashes but nothing happens.

        imageTagsInput.addTextChangedListener(object : TextWatcher {

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                imageTagsInput.removeTextChangedListener(this)
                imageTagsInput.setText(imageTagsInput.text.toString().replace(" ","-"))
                imageTagsInput.addTextChangedListener(this)
                imageTagsInput.setSelection(imageTagsInput.length())


            }

            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

        })

And this is the xml of the EditText: android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-  "

Tsabary
  • 3,119
  • 2
  • 24
  • 66
  • remove this thing from your xml android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-  " It might work now – primo May 02 '19 at 04:41
  • But I want to limit what characters could be used (Latin characters, numbers and some other specific characters). – Tsabary May 02 '19 at 04:47
  • you can do it other too but just remove and try whether you get what you want or not – primo May 02 '19 at 04:54
  • https://stackoverflow.com/a/21828520/8101634 see this link it will help you restrict your characters – primo May 02 '19 at 04:58
  • @primo I just tried removing it, it still doesn't work – Tsabary May 02 '19 at 05:00
  • keep it as it is how you kept your xml file and then see my answer – primo May 02 '19 at 05:08

1 Answers1

1

Add something like this in afterTextChanged

  String result = s.toString().replaceAll(" ", "-");
    if (!s.toString().equals(result)) {
         ed.setText(result);
         ed.setSelection(result.length());
         // alert the user
    }

For more info see this answer

primo
  • 1,340
  • 3
  • 12
  • 40
  • It doesn't work. Both this and my original code work with other characters, it's something to do with the space I believe – Tsabary May 02 '19 at 05:20
  • you can do something like detect if spacebar is pressed then replace it with dash – primo May 02 '19 at 05:22
  • 1
    I found a way around my issue that works for me. I needed the text to change to compare it with existing tags. While it doesn't work for me for changing the actual text, I am just using it to change the text I use for the comparison, so while the user is typing spaces the search functions searched for the same string only with dashes instead. I appreciate the effort. – Tsabary May 02 '19 at 05:28
  • add your answer if you found out your solution – primo May 02 '19 at 05:29
  • I haven't found the solution, just found a different approach that satisfies my goal, but it doesn't solve my original issue. – Tsabary May 02 '19 at 05:44