0

i have created a copy button succesfully that copy my textview but when i try it out it doesn't work even though the text copied to clipboard appears but still the textview haven't been copied

this is my code in SecondActivity.java containing the copy button

button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {


public void onClick(View arg0) { getSystemService(CLIPBOARD_SERVICE); 
Toast.makeText(getApplicationContext(), "Text copied to clipboard", Toast.LENGTH_SHORT).show();        



}
});

}

i hope you can help me and thanks in advance

user3135137
  • 37
  • 1
  • 4

1 Answers1

0

Try fetching the clipboard text like this and see if you can get the text:

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
String strCopiedText = clipboard.getText();

Once you are able to fetch the text set where ever you want to.

In case you want a desired text to be saved in the clipboard than use:

clipboard.setText(yourtextView.getText().toString());

Edited:

button = (Button) findViewById(R.id.button1); 
button.setOnClickListener(new View.OnClickListener() { 
    @SuppressWarnings("deprecation") 
    public void onClick(View v) { 

        ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
        String strCopiedText;
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
            ClipData clip = ClipData.newPlainText("label", "Text to Copy");
            clipboard.setPrimaryClip(clip); 

            strCopiedText = (String) clipboard.getText(); 
            clipboard.setText(state2.getText().toString());

        } else{
            strCopiedText = (String) clipboard.getText(); 
            clipboard.setText(state2.getText().toString());
        }
    }
}
Tabrej Khan
  • 894
  • 6
  • 15
  • this is what i did button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @SuppressWarnings("deprecation") public void onClick(View v) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); @SuppressWarnings({ "unused" }) String strCopiedText = (String) clipboard.getText(); clipboard.setText(state2.getText().toString()); – user3135137 Dec 28 '13 at 14:54
  • the app crah when i ress the copy button – user3135137 Dec 28 '13 at 14:55
  • i have min=11 max=19 and i'm using with a device that have api level 15 – user3135137 Dec 28 '13 at 19:20