Reputation: 41
I have this code for selecting a textView and copying to clipboard :
txt=(TextView)findViewById(R.id.textView1);
String stringYouExtracted = txt.getText().toString();
int startIndex = txt.getSelectionStart();
int endIndex = txt.getSelectionEnd();
stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
I want put a button that when I press it , sending text enables and runs , I have this code too :
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, stringYouExtracted);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
But this error appears from setOnClickListener
(3rd line of setOnClickListener
):
Cannot refer to a non-final variable stringYouExtracted inside an inner class defined in a different method
The SDK suggests me to add final before second line of first code. When I do this another error from 5th line of first code appears:
The final local variable stringYouExtracted cannot be assigned. It must be blank and not using a compound assignment
and suggests me to remove final from second line of first code that I have added it for solving previous error
What can I do?
Upvotes: 1
Views: 126
Reputation: 2445
Try this:
String value = txt.getText().toString();
int startIndex = txt.getSelectionStart();
int endIndex = txt.getSelectionEnd();
final String stringYouExtracted = value.substring(startIndex, endIndex);
Upvotes: 1
Reputation: 338
Remove:
String stringYouExtracted = txt.getText().toString();
Change from
stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
to
final String stringYouExtracted = txt.getText().toString().substring(startIndex, endIndex);
Upvotes: 1