1

I am creating a notepad application in android. I have given a functionality to share individual note with other apps. I want the share function to share the title and content of the note. I can't seem to make it work. Below is my java code for share intent.

JAVA

    String title=noteModel.getTitle();
    String content=noteModel.getContent();
    Intent intentShare = new Intent();
    intentShare.setAction(Intent.ACTION_SEND);

    intentShare.putExtra(Intent.EXTRA_TEXT,title);
    intentShare.putExtra(Intent.EXTRA_TEXT,content);            
    intentShare.setType("text/plain");
    context.startActivity(intentShare.createChooser(intentShare,"Send note to"));
  • what is the problem u r facing? – Mohd Asif Ahmed Aug 22 '17 at 05:56
  • I am unable to pass two values. In the above code only "content" value gets displayed on the screen to be share but not "title". I have tried using a different key - `EXTRA_SUBJECT` for title and `EXTRA_TEXT` for content, it doesn't work either. – Pawanjot Singh Aug 22 '17 at 15:14

4 Answers4

2

I found a way to pass multiple strings with just one EXTRA_TEXT. I wanted to pass two values, namely, title and content, So I stored the value of title in a string called "title" and the value of content in a string called "content". Now, the trick ! I concatenated both the strings together and stored that concatenated string to a new string and passed that string into EXTRA_TEXT.

I am gonna share my correct code just to give a clear picture.

String title=noteModel.getTitle();
String content=noteModel.getContent();
String titleAndContent="Title: "+title+"\n Content: "+content;

Intent intentShare = new Intent();
intentShare.setAction(Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare.putExtra(Intent.EXTRA_TEXT,titleAndContent);

context.startActivity(intentShare);
0

You used the same key for two Extra

    intentShare.putExtra(Intent.EXTRA_TEXT,title);
    intentShare.putExtra(Intent.EXTRA_TEXT,content);   

You have to use a different key for every extra you pass. Change the content extra similar to below one.

intentShare.putExtra(Intent.EXTRA_CONTENT,content);
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
0
intentShare.putExtra(Intent.EXTRA_TEXT,title);
intentShare.putExtra(Intent.EXTRA_TEXT,content); ///ovewriting value

please use different key

phpdroid
  • 1,642
  • 1
  • 18
  • 36
0

try this

intentShare.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
intentShare.putExtra(android.content.Intent.EXTRA_TEXT, content);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163