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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3410344
  • 41
  • 13

2 Answers2

1

Remove:

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

Change from

stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);

to

final String stringYouExtracted = txt.getText().toString().substring(startIndex, endIndex);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Selva
  • 338
  • 2
  • 12
  • 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 ??? – user3410344 Mar 31 '14 at 07: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 '14 at 07: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 ehsanhanifezade@yahoo.com ..... thank you – user3410344 Mar 31 '14 at 07:29
  • I could .......... (http://stackoverflow.com/questions/22756458/why-doesnt-this-code-send-anything-to-other-applications-it-just-opens-other) ......... please answer it – user3410344 Mar 31 '14 at 08:08
1

Try this:

String value = txt.getText().toString();
int startIndex = txt.getSelectionStart();
int endIndex = txt.getSelectionEnd();
final String stringYouExtracted = value.substring(startIndex, endIndex);
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
  • I did but this error appears for last line of your code : " Syntax error on token "final", invalid Type " – user3410344 Mar 31 '14 at 07:11
  • sorry i missed the declaration see the edit: final String stringYouExtracted – Luca Sepe Mar 31 '14 at 07: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 – user3410344 Mar 31 '14 at 07:33
  • look here: http://stackoverflow.com/questions/9948373/android-share-plain-text-using-intent-to-all-messaging-apps maybe will help you – Luca Sepe Mar 31 '14 at 07:41
  • It doesn't work too ...... please answer me here : (http://stackoverflow.com/questions/22756458/why-doesnt-this-code-send-anything-to-other-applications-it-just-opens-other) – user3410344 Mar 31 '14 at 08:19