3

I have a set of four EditText views that are used to input a 4 digit code. Each one of these is set to a maxLength of 1, because they hold a single of those digit.

Now I want to allow my users to copy the four digit code and paste it directly into the 4 fields.

I have tried detecting the paste event using:

@Override
    public boolean onTextContextMenuItem(int id) {
        boolean consumed = super.onTextContextMenuItem(id);
        switch (id){
            case android.R.id.cut:
                onTextCut();
                break;
            case android.R.id.paste:
                onTextPaste();
                break;
            case android.R.id.copy:
                onTextCopy();
        }
        return consumed;
    }

like in this question, but I have no way of returning the pasted text in the callback.

I also tried with:

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

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

But out of the text being pasted I only get 1 char, I guess because the maxLength is set to 1.

How can I achieve the desired behavior?

MichelReap
  • 5,630
  • 11
  • 37
  • 99

1 Answers1

1

Maybe this can help you

 private void pasteText() {
        ClipboardManager clipboardManager = (ClipboardManager)
                getSystemService(Context.CLIPBOARD_SERVICE);

        if(clipboardManager.hasPrimaryClip()) {
            ClipData.Item item = clipboardManager.getPrimaryClip().getItemAt(0);

            CharSequence ptext = item.getText();
            for(int i = 0 ; i <= ptext.length() ; i++){
    // 4 cases and paste to 4 edittexts
    }
        }
    }
keser
  • 2,472
  • 1
  • 12
  • 38
  • Hi @keser can you explain more, please? – Abbas Jafari Feb 14 '21 at 18:12
  • @abbasjafary this is a really old question and i havent developed android in a long time. But i'll try to help. What's your problem? – keser Feb 15 '21 at 06:31
  • Thanks for your reply, I have six edit text for entering six-digit code, and I want to that when a user copy and past six-digit code six edit text automatically filling. I hope you understand what I mean – Abbas Jafari Feb 15 '21 at 07:18