0

I am trying to change space to % in my android code. My aim is like this:

when user writes for example "I am trying", after press spacebar instead a gap between words a % be written in myeditText; I mean like this: "I%am%trying"

majid
  • 29
  • 4
  • It's Simple... Just listen for `keyup` event on Activity/Fragment or the `EditText` where you want the text to appear.. For more Info you should see: [This Question](https://stackoverflow.com/questions/7305972/edittext-onkeydown) – Syed Ali Naqi Jul 09 '19 at 03:30

2 Answers2

0

Just implement a TextWatcher on your editText and inside its onTextChanged() method implements a simple if condition to check if space is entered or not and then replace that space with % if the condition is true.

Kuldeep Rathee
  • 282
  • 2
  • 7
0

try this,

editText.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 (charSequence.toString().equalsIgnoreCase(" ")) {
                editText.setText(charSequence.toString().replace(" ", "%"));
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
  • Hi thanks for Answer. I used that but when I press first space, replace " " to "%" but when press another " " there is no feedback – majid Jul 09 '19 at 17:27
  • no. when i write for example "I am trying" , there is no any change and doesnt come % after push space key – majid Jul 12 '19 at 14:14