2

I have a EditText - ed1. I want to copy the contents of ed1to clipboard, concat it with Hello World and paste it to another EditText - ed2 on the tap of a button. But, I am getting some additional data along with the contents on ed1.

final EditText ed1 = (EditText) findViewById(R.id.editText1);
    final EditText ed2 = (EditText) findViewById(R.id.editText2);
    Button b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
            String add = "Hello World";
            ClipData clip = ClipData.newPlainText("", ed1.getText().toString().concat(" "+add));
            clipboard.setPrimaryClip(clip);
            ed2.setText(clip.toString());

        }
    });



enter image description here

CodeWalker
  • 2,281
  • 4
  • 23
  • 50

1 Answers1

0

Just add following lines:

ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
edt2.setText(item.getText());

So, your final code for button listener, where you are copying data from clip board should looks like below:

btn.setOnClickListener(new OnClickListener() {          
        @Override
        public void onClick(View arg0) {        

             ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
                String add = "Hello World";
                ClipData clip = ClipData.newPlainText("", ed1.getText().toString().concat(" "+add));
                clipboard.setPrimaryClip(clip);
                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
                ed2.setText(item.getText());                
        }
    });
AADProgramming
  • 6,077
  • 11
  • 38
  • 58