Can anybody please tell me how to copy the text present in a particular textview to clipboard when a button is pressed? Thanx :)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
textView = (TextView) findViewById(R.id.textview);
copyText = (Button) findViewById(R.id.bCopy);
copyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String getstring = textView.getText().toString();
//Help to continue :)
}
});
}
}
I want to copy the Text in TextView textView to clipboard when the Button bCopy is pressed! Please do help...
Yesterday I made this class. Take it, it's for all API Levels
As a handy kotlin extension:
Just use this. It works only for android api >= 11 before that you'll have to use a ClipData.
Hope it helped you :)
[UPDATE 3/19/2015] Just like Ujjwal Singh said it the method
setText
is deprecated now, you should use, just as the docs recommande it, setPrimaryClip(clipData)use ClipboardManager
make sure you have imported
android.content.ClipboardManager
and NOTandroid.text.ClipboardManager
. Latter is deprecated. Check this link for Further information.use this function for copy to clipboard
int sdk = android.os.Build.VERSION.SDK_INT;