Msk
Msk

Reputation: 857

My app option in 'share with' from contextual action bar of any app

I have built a dictionary app which does what is says that is find meaning of words.

I have noticed that the 'share with' option (if present) in a contextual action bar of any app gives a list of apps, with which the selected data can be shared.

For ex. In a browser on long pressing of text and selecting share with in the contextual action bar i can share with 'Keep' app to create a note. I want to enlist my app here so that the user can directly find the meaning of selected text.

I understand that i will have to implement some sort of listener and maybe android will autolist my app, but am unable to find the exact steps. I have seen some apps other than system apps in the share option, so i believe it can be done for any app.

Could someone give me directions for the same.

Also is there any way that i can recognize some unique trigger, example a triple click on word in any app and the fragment of my dictionary pops up which shall contain the meaning. I know that this last part will mostly is not possible to achieve but still?

Upvotes: 0

Views: 48

Answers (1)

Mattia Maestrini
Mattia Maestrini

Reputation: 32790

Add this to your AndroidManifest.xml

<activity android:name=".ui.MyActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

MyActivity.java

void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
            if (sharedText != null) {
                // Update UI to reflect text being shared
            }
        } 
    }
}

The complete tutorial can be found here:
https://developer.android.com/training/sharing/receive.html

Upvotes: 1

Related Questions