0

I want to add comma when I enter double space from keyboard in multiautocompletetextview. I search lots of thing in google. But can't reach my goal. I want to replace comma for double space entering by user.

So obviously, I must have to write something logic in ontextChange() or OnAfterTextChanged() in addtextwatcher listener.but i do't got event of after add 2 space.

I have already used comma tokenizer when select word from list.but i want to add comma when user entering double space using keypad.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dipali
  • 10,966
  • 5
  • 25
  • 51

2 Answers2

1

Try like this, I have not tried this code

boolean userPressedKey = false ;
int spaceCount = 0;


yourEditText.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
        userPressedKey = false ;
    }

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

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        userPressedKey = true;
    }); 


public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (userPressedKey) {
        if (keyCode == KeyEvent.KEYCODE_SPACE) {
            spaceCount ++;
            if(spaceCount == 2){
                 //append comma to the edittext here
                 Toast.makeText(MainActivity.this, "White space is clicked twice", Toast.LENGTH_LONG).show();
            }

            return true;
        }else{
            spaceCount=0;
        }
    }
    super.onKeyDown(keyCode, event);
}
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
1

The simplest solution i can provide you is to use String.replace(), Here is small code snippet to help you

 @Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    edt.addTextChangedListener(textWatcher);
}

And TextWatcher which you are going to set on EditText

TextWatcher textWatcher = 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) {
        edt.removeTextChangedListener(textWatcher);
        String text = edt.getText().toString();
        text = text.replace("  ", ",");
        edt.setText(text);
        edt.setSelection(text.length());
        edt.addTextChangedListener(textWatcher);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

enter image description here

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • i am using multiautocompletetextview so i can not set text in this .provide solution for this. – dipali Nov 10 '16 at 07:15
  • you can do it, i have done this code and used `MultiAutoCompleteTextView` and its working perfectly, as you can see in [docs](https://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html) also `MultiAutoCompleteTextView` is extending `EditText` only so you can use all property of `EditText` – Ravi Nov 10 '16 at 07:19