Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste into multiple edittext

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?

like image 207
MichelReap Avatar asked Mar 06 '26 22:03

MichelReap


1 Answers

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
    }
        }
    }
like image 134
keser Avatar answered Mar 09 '26 13:03

keser