Android: Share Plain Text Using Intent (To All Messaging Apps)

Android: Share plain text using intent (to all messaging apps)

Use the code as:

    /*Create an ACTION_SEND Intent*/
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
/*This will be the actual content you wish you share.*/
String shareBody = "Here is the share content body";
/*The type of the content is text, obviously.*/
intent.setType("text/plain");
/*Applying information Subject and Body.*/
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
/*Fire!*/
startActivity(Intent.createChooser(intent, getString(R.string.share_using)));

Passing Multiple text/plain values in share intent

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);

generate image and share with intent without permission

After some researches using the suggestion of CommonsWare I managed to make it work with the following code:

TextPaint tPaint = new TextPaint();
tPaint.setTextSize(30);
tPaint.setColor(Color.BLACK);
tPaint.setStyle(Paint.Style.FILL);

StaticLayout layoutBody = new StaticLayout(textToShare, tPaint, 600, Layout.Alignment.ALIGN_CENTER, 1, 1, false);
Bitmap image = Bitmap.createBitmap(layoutBody.getWidth(), layoutBody.getHeight(), Bitmap.Config.ARGB_8888);

Canvas cs = new Canvas(image);
layoutBody.draw(cs);
cs.drawBitmap(dest, 0, 0, null);

Followed by what I found here posting it here in case it helps anyone



Related Topics



Leave a reply



Submit