Overriding title for an Android share Intent - android

It seems that Android automatically sets the title of sharing intents to the name of the app. I would like to override it to make it a little more descriptive (e.g. instead of "My App", the title could be "Preview with My App".
How can I change the title in the sharing dialogue pop up box in Android?
I have added the following code to my AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>

The key is to start your intent as an activity by passing it as an argument to Intent.createChooser(intent: Intent)
Here's an example in Kotlin. We all love Kotlin dont we? :)
Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_TEXT, "Some Text")
type = "text/plain"
startActivity(Intent.createChooser(this, "My Title"))
}

<intent-filter> supports the attributes android:icon and android:label. You can use them to change the icon and label that are displayed in the system's Share UI.
Please note that (at least on Android 10) the app name is still displayed. So instead of "Preview with My App" you maybe want to just use "Preview" as label.
Example:
<intent-filter
android:icon="#drawable/share_icon"
android:label="#string/share_label">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
See https://developer.android.com/guide/topics/manifest/intent-filter-element

The title has to come along with the Share Intent. So, if your application is one of the apps that can show a preview, then there is nothing you can do to show the title for choosing your app(it has to come from the calling app).
On the other hand, if you are using the Share Intent in your application itself, you can try this:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "What ever text you cant to share");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Preview with:")); <---- here
Check the description for createChooser

Related

Unable to see the values set in EXTRA_TITLE

I am building an app say Tx , which call's the createChooser using the below code.
Intent shareIntent = new Intent("com.myapplication.test");
shareIntent.putExtra(Intent.EXTRA_TITLE, "Sharing ..");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Shared from Tx app.." );
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent,"Choose the app ..")));
I have also built few app's with below in their androidManfiest.xml files.
<intent-filter>
<action android:name="com.myapplication.test" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
When Tx's createChooser is called, I am able to see all the app's where -com.myapplication.test is present & also the title as - Choose the app .. . However, I am not able to see the EXTRA_TITLE which is set to "Sharing ..".
Is there any way I can show this in the chooser? any help would be highly appreciated.

Use intent ACTION_SEND to sharing data from anchor link to specific app

I want to share data from my website to Android app using a single link.
For example when user click on the below link:
Share...
the browser launch Telegram app and ask user to select a contact to send EXTRA_TEXT to him/her. But the above code only open the telegram app and doesn't pass the EXTRA_TEXT to app.
I found this guide but this is JAVA and I don't know how to use this code in a link:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Put the data scheme and parameters in the anchor tag.For e.g.:
myappschema://?token=123
where it will send token to the myappschema.
In the AndroidManifest of your app; put this intent filter on your activity which you wish to open from web.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_BROWSER" />
<data android:scheme="myappschema" />
</intent-filter>
Inside your activity; you can fetch data from Intent.
eg: Uri data =getIntent().getData();

My Custom Intents do not work. Seems flakey

I am attempting to get one activity to launh another via an intent and intent filter. Here is what I have.
In the launching activity:
Intent i = new Intent();
i.setAction("com.test.apps.CATAPP");
i.addCategory("com.test.apps");
this.startActivityForResult(i,APP_REQUEST_CODE);
The intent filter in the receiving app:
<intent-filter>
<action android:name="com.test.apps.CATAPP" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.test.apps" />
<data android:mimeType="text/plain"/>
</intent-filter>
This fails to work. However, if I take the example from Wei Meng Lee's Android 4 Development, it works and then only if the URI and the Intent.ACTION_VIEW are present.
Code in the launching app:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
i.setAction("com.test.apps.CATAPP");
i.addCategory("com.test.apps");
this.startActivityForResult(i,APP_REQUEST_CODE);
Intend in the receiving app:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="com.test.apps.CATAPP" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.test.apps" />
<data android:scheme="http"/>
</intent-filter>
Where am I going wrong? Have I made a simple mistake?
I have spend the last couple of hours trawling stack overflow, for anwer to this but only found similar questions, with answers that did not solve my problem or were not applicable.
In your first example, your Intent is not going to match the IntentFilter of the Activity. You've specified a data element in the IntentFilter, but you've not set one on the Intent. You can either set the MIME type on the Intent:
i.setType("text/plain");
Or remove the <data> element from the <intent-filter> in the manifest, depending on your requirements.
If you just want to pass some simple text, set the Intent's type as shown above, and attach a String extra to it.
i.putExtra(Intent.EXTRA_TEXT, "Plain text.");
Then, to retrieve the text in the next Activity:
String plainText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
if(plainText != null)
...
Note that the Intent.EXTRA_TEXT constant is merely a convenience. The extra key can be anything you want, as long as it's the same in both places.

Add custom action to Android Share-Sheet

I have an app where the user should be able to share some text. Now I want to provide the default sharing options for plain text that Android provides. I do so with the following code:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
Intent chooser = Intent.createChooser(sendIntent, "Share");
startActivity(chooser);
This will look a bit like that:
Source: http://developer.android.com/training/basics/intents/sending.html
But now I also would like to be able to have one more option in the Share-Service-Picker Dialog that triggers a custom action in my own code. Namely I want the user to be able to favourite an entry. So beside sharing via SMS, Email, FB, whatever, I'd like there to be one more item on the top of that list, saying "Add to favourites" (Including an icon if possible).
So my question is if that's possible?!? And if, how :)
Any tips are appreciated!
Intent filters inform the system what intents an application component is willing to accept. Similar to how you constructed an intent with action ACTION_SEND in the Sending Simple Data to Other Apps lesson, you create intent filters in order to be able to receive intents with this action. You define an intent filter in your manifest, using the element. For example, if your application handles receiving text content, a single image of any type, or multiple images of any type, your manifest would look like:
<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
from Receiving Simple Data from Other Apps:Update Your Manifest

Android: share CSV file

My aplication can generate CSV files that I want to share. I'm using MIME type text/comma_separated_values/csv, but when I send the Intent the chooser isn't shown, I guess my device doesn't know how to handle the file. Which type should I use?
This is my code:
Uri csv = lh.createDailyCSV();
if(csv == null){
Toast.makeText(this, getString(R.string.error_creating_csv), Toast.LENGTH_LONG).show();
}
else{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/comma_separated_values/csv");
sharingIntent.setData(csv);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.send_to)));
}
I declared in my manifest:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/comma_separated_values/csv" />
</intent-filter>
And I get the exception
03-12 12:19:23.430: E/ActivityThread(24011): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1#412fc920 that was originally registered here. Are you missing a call to unregisterReceiver()?
I've read this Exception occurs when there is none or just 1 option in the chooser.
[EDIT]
I changed how I attach the data to the Intent. Instead of sharingIntent.setData(csv) I used:
sharingIntent.putExtra(Intent.EXTRA_STREAM, csv);
And now the chooser works fine, but if I try to send the file via e-mail I get an error: File couldn't be shown.
[/EDIT]
The correct MIME type would be text/csv. If that doesn't work, you can use text/plain which will allow the user to potentially choose from a lot of apps, including Evernote etc.
Update After the update, it seems that you don't want to "share" the file with arbitrary other apps but only send it by e-mail? Please clarify.
In my case, the following works in Goo:
intent.setType("text/*");
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
<data android:mimeType="text/*"/>
</intent-filter>

Categories

Resources