1

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?

2 Answers 2

1

Remove:

String stringYouExtracted = txt.getText().toString();

Change from

stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);

to

final String stringYouExtracted = txt.getText().toString().substring(startIndex, endIndex);
4
  • It has a problem .... when I press the button and choose an application for example viber ,, you see just viber opens and my text that I had selected it from first code doesn't send to viber ... what can I do ??? Mar 31, 2014 at 7:06
  • Answer for "How can I fix this code about sending intent?" question is provided i think so. You have to open a new question for the thing mentioned in the above comment.
    – Selva
    Mar 31, 2014 at 7:10
  • excuse me ... I tried to ask another question but " You can only ask 6 questions in a 24-hour period " appears ........ I can ask question 8 hours later ...... can u answer it here please ??? or send your answer to [email protected] ..... thank you Mar 31, 2014 at 7:29
  • I could .......... (stackoverflow.com/questions/22756458/…) ......... please answer it Mar 31, 2014 at 8:08
1

Try this:

String value = txt.getText().toString();
int startIndex = txt.getSelectionStart();
int endIndex = txt.getSelectionEnd();
final String stringYouExtracted = value.substring(startIndex, endIndex);
5
  • I did but this error appears for last line of your code : " Syntax error on token "final", invalid Type " Mar 31, 2014 at 7:11
  • sorry i missed the declaration see the edit: final String stringYouExtracted
    – Luca Sepe
    Mar 31, 2014 at 7:21
  • you're welcome but It has a problem .... when I press the button and choose an application for example viber ,, you see just viber opens and my text that I had selected it from first code doesn't send to viber ... what can I do ??? please help me Mar 31, 2014 at 7:33
  • look here: stackoverflow.com/questions/9948373/… maybe will help you
    – Luca Sepe
    Mar 31, 2014 at 7:41
  • It doesn't work too ...... please answer me here : (stackoverflow.com/questions/22756458/…) Mar 31, 2014 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.