Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement share action in android?

Tags:

android

I want to implement share action in my project but when I used MenuItem it's giving Runtime error to use MenuItemCompat instead, but its too giving error.

Here is my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_third_, menu);
    MenuItem menuItem = menu.findItem(R.id.menu_item_share);


    mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return true;
}

public Intent getDefaultShareIntent (){

   Intent shareIntent = new Intent(Intent.ACTION_SEND);

    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
    shareIntent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
    return shareIntent;
}
like image 995
Jenifer Munir Avatar asked Nov 08 '15 11:11

Jenifer Munir


People also ask

How can I share data between two apps on Android?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.

How implement rate it feature in Android app?

Step 1: Add the support Library in build. gradle file and add Android-Rate dependency in the dependencies section. This library have a function which redirects the user to google play store and allows them to rate the application.


1 Answers

I believe you have subclassed AppCompatActivity for your activity due to which you are getting this error, Kindly use following code

Initialize ShareActionProvider in your activity

import android.support.v7.widget.ShareActionProvider;
import android.support.v4.view.MenuItemCompat;


    private ShareActionProvider mShareActionProvider;

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.action_share);

        // Fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

        mShareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        // Set share Intent.
        // Note: You can set the share Intent afterwords if you don't want to set it right now.
        mShareActionProvider.setShareIntent(createShareIntent());
        // Return true to display menu
        return true;
    }

    // Create and return the Share Intent
    private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "http://play.google.com/store/apps/details?id=pk.nimgade.Bostan.Train.Schedule");
        Intent intent = Intent.createChooser(shareIntent,"Share");
        return shareIntent;
    }

and this is how your xml view would be like

 <item
        android:id="@+id/action_share"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        app:showAsAction="always"
        android:title="Share" />

please make sure your SDK is updated, In this peculiar case I did run into that, It is nasty when you don't know what is missing

like image 173
Pankaj Nimgade Avatar answered Sep 22 '22 14:09

Pankaj Nimgade