How can I add a share button on my android app

11,829

Firstly, modify your menu.xml file to include the share-action menu item:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_share"
        android:icon="@drawable/share"
        app:showAsAction="ifRoom" />
       <-- here is the share action menu item -->
       <item
            android:id="@+id/menu_item_share"
            app:showAsAction="ifRoom"
            android:title="Share"
            app:actionProviderClass=
            "android.support.v7.widget.ShareActionProvider" />
</menu>

Then change your onCreateOptionsMenu by adding the ShareActionProvider code based on the share-action menu item:

 //add the following imports (you may already have them)
    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.ShareActionProvider;
    ...
    private ShareActionProvider mShareActionProvider;
    ...
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

      getMenuInflater().inflate(R.menu.menu_main, menu);
      MenuItem item = menu.findItem(R.id.menu_item_share);
      mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
      //create the sharing intent
      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
      sharingIntent.setType("text/plain");
      String shareBody = "here goes your share content body";
      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share Subject");
     sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

     //then set the sharingIntent
     mShareActionProvider.setShareIntent(sharingIntent);
            return true;
  }

I hope this helps you. Please give it a try and let me know. You can also consult these closely related links:

Adding an Easy Share Action

Activating Share Button in Android

Basic sharing example

Example of using appcompat-v7 ShareActionProvider

Share:
11,829
Hessel
Author by

Hessel

i'm 15 years old

Updated on June 14, 2022

Comments

  • Hessel
    Hessel almost 2 years

    I want to add a share button on my app.

    If users use the app there would be an option that they can share my app, but unfortunately it's not happening.

    Here is my code:

    MainActivity.java

    package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ShareActionProvider;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.webkit.WebView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            String url ="http://dehvb.nl/";
            WebView view=(WebView) this.findViewById(R.id.webView);
            view.getSettings().setJavaScriptEnabled(true);
            view.getSettings().setBuiltInZoomControls(true);
            view.getSettings().setDisplayZoomControls(false);
            view.loadUrl(url);
    
        }
    
        private ShareActionProvider mShareActionProvider;
        @Override
    
        public boolean onCreateOptionsMenu(Menu menu) {
    
            getMenuInflater().inflate(R.menu.menu_main, menu);
            MenuItem item = menu.findItem(R.id.menu_item_share);
            mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "here goes your share content body";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share Subject");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    
            //then set the sharingIntent
            mShareActionProvider.setShareIntent(sharingIntent);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.menu_item_share) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    menu_main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity">
    
        <item
            android:id="@+id/menu_item_share"
            app:showAsAction="ifRoom"
            android:title="Share"
            android:icon="@drawable/share"
            android:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
    
    </menu>
    

    LogCat:

    06-10 14:37:09.841 24733-24733/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ed7b38d time:33915488
    06-10 14:37:15.501 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_Ime: ImeThread is not enabled.
    06-10 14:37:15.511 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/Activity: performCreate Call Injection manager
    06-10 14:37:15.511 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOnViewCreated > Target : com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity isFragment :false
    06-10 14:37:15.521 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
    06-10 14:37:15.531 28065-28196/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
    06-10 14:37:15.531 28065-28196/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xdccbf864
    06-10 14:37:15.541 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
    06-10 14:37:15.541 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
    06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xdd13fc54
    06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: Initialized EGL, version 1.4
    06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0xeedbea00 ,&mEglDisplay = 1 , &mEglConfig = -189468004 
    06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
    06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Enabling debug mode 0
    06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/mali_winsys: new_window_surface returns 0x3000,  [1080x1920]-format:1
    06-10 14:37:15.791 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchCreateOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
    06-10 14:37:15.791 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchPrepareOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
    06-10 14:37:15.901 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ed7b38d time:33921540
    06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28065
    06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
    06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
    06-10 14:37:21.811 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
    06-10 14:37:21.881 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
    06-10 14:37:22.261 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
    06-10 14:37:22.361 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
    06-10 14:40:49.801 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/AndroidRuntime: Shutting down VM
    06-10 14:40:49.801 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                 Process: com.vvhvb.hesselfeenstra.vvheerenveenseboys, PID: 31316
                                                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ShareActionProvider.setShareIntent(android.content.Intent)' on a null object reference
                                                                                                     at com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity.onCreateOptionsMenu(MainActivity.java:48)
                                                                                                     at android.app.Activity.onCreatePanelMenu(Activity.java:3124)
                                                                                                     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
                                                                                                     at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                                     at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                                                                     at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                                     at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                                                                     at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                     at android.os.Looper.loop(Looper.java:145)
                                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6917)
                                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
    06-10 14:40:54.201 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Process: Sending signal. PID: 31316 SIG: 9