2

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
Draken
  • 3,134
  • 13
  • 34
  • 54
Hessel
  • 437
  • 2
  • 5
  • 11

1 Answers1

8

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

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • I get a error by (ShareActionProvider) item.getActionProvider(); how can i delete this error? – Hessel Jun 10 '16 at 07:05
  • 1
    it gives by line: mShareActionProvider = (ShareActionProvider) item.getActionProvider(); the next error: incornvertible types; cannot cast 'android.view.actionprovider' to 'android.support.v7.widget.ShareActionProvider' – Hessel Jun 10 '16 at 07:22
  • You probably imported the wrong `ShareActionProvider` - please add the import for `android.widget.ShareActionProvider` instead. – ishmaelMakitla Jun 10 '16 at 07:29
  • Great! Please vote and accept the answer if you find the answer helpful. – ishmaelMakitla Jun 10 '16 at 07:42
  • Do you get any errors with that? Please paste the error log - this might help us figure out what the issue is. – ishmaelMakitla Jun 10 '16 at 09:08
  • i dont have errors but the app stops when i open it... ( i cant paste the logcat here ) – Hessel Jun 10 '16 at 09:24
  • 1
    If there are errors or the app crashes - there should be an error log. Please check - you can then edit your question and paste the logcat error log there. – ishmaelMakitla Jun 10 '16 at 11:16
  • I have add a logcat list. If you need more i will add it. – Hessel Jun 10 '16 at 11:34
  • I see the error - it is because you are using `AppCompatActivity` - let me update my answer. – ishmaelMakitla Jun 10 '16 at 12:13
  • Okay. I cant see it on the logcat why the error is showing – Hessel Jun 10 '16 at 12:16
  • I have change the code but it still not works? I have updated my logcat! – Hessel Jun 10 '16 at 12:43
  • It's a `NullPointerException` because somehow the `getActionProvider(item)` returns `null` - please show your updated code, including the Menu XML file. I want to confirm that all changes have been made - – ishmaelMakitla Jun 10 '16 at 12:54
  • Thanks for the updates, the mistake was that we left out the ***namespace* - so I used `app` namespace instead in declaring the `actionProviderClass`. Please give it a try - it should now return an actual `ShareActionProvider` instead of `null`. – ishmaelMakitla Jun 11 '16 at 10:21
  • Were you able to try out the updated code? Please let me know if it works for you - or what errors are you getting? – ishmaelMakitla Jun 12 '16 at 17:25