Good day.
I want to make sure that by clicking on the button the contents of the TextView copied to the buffer.
Button copyButton = (Button)findViewById(R.id.copyButton); TextView generatedCode = (TextView) findViewById(R.id.generatedCode); String stringYouExtracted = generatedCode.getText().toString(); int startIndex = generatedCode.getSelectionStart(); int endIndex = generatedCode.getSelectionEnd(); stringYouExtracted = (stringYouExtracted).substring(startIndex, endIndex); if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(stringYouExtracted); } else { android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringYouExtracted); clipboard.setPrimaryClip(clip); } This code does not work correctly because of the line:
stringYouExtracted = (stringYouExtracted).substring(startIndex, endIndex); throws a ClassCastException . Thank you in advance.