1

I have a RecyclerView which has many CardViews. Each CardView has a unique ID.

I need to add a button to each CardView so that when the user clicks this button he can share a simple link in the format of "www.domain.com/xx=ID" via Whatsapp or messenger or any other application.

I tried the following code:

holder.myBTN.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "text to be shared";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
});

but I am getting the following error:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
V/FA: Inactivity, disconnecting from the service
D/AndroidRuntime: Shutting down VM
E/UncaughtException: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
    at com.myapp.myapp_rental.PropertiesAdapter$4.onClick(PropertiesAdapter.java:229)
    at android.view.View.performClick(View.java:5181)
    at android.view.View$PerformClick.run(View.java:20887)
    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:5942)
    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:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=ListingActivity, _si=-3768936060483324961, timestamp=1484388033695, fatal=1}]
V/FA: Using measurement service
V/FA: Connecting to remote service
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.myapp_rental, PID: 20082
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
    at com.myapp.myapp_rental.PropertiesAdapter$4.onClick(PropertiesAdapter.java:229)
    at android.view.View.performClick(View.java:5181)
    at android.view.View$PerformClick.run(View.java:20887)
    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:5942)
    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:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
    I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
    W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
    D/ResourcesManager: creating new AssetManager and set to /data/app/com.google.android.gms-2/base.apk
    W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
    W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Adham
  • 87
  • 1
  • 8

2 Answers2

1

For me, there is an easy solution for what you have to do.

In layout of Cardview, you can add a Toolbar who contains an Easy Share Action : CardView toolbars

In your RecyClerView, each time you called the onBindViewHolder, you can assign items in your menu https://developer.android.com/training/sharing/shareaction.html with link and ID you want.

If you have trouble with RecyclerView and CardView, follow this guide : https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156

Edit : If you want to share via WhatsApp see : https://www.whatsapp.com/faq/en/android/28000012 or use API like https://developers.facebook.com/docs/messenger/android

I hope I have helped. :)

Community
  • 1
  • 1
J-Jamet
  • 847
  • 8
  • 17
  • i want all apps not only whatsApp. – Adham Jan 14 '17 at 10:26
  • my code work fine from activity but when i use it inside cardview it give me the error – Adham Jan 14 '17 at 10:27
  • Ha ok, maybe a problem with API of CardView, domains may be different, it's weird. Why do you use "android.content.Intent" one time ans just "Intent" another time ? It's the same package ? – J-Jamet Jan 14 '17 at 10:32
0

The share buttons you see normally are mostly ImageViews with an onClick attribute assigned to them. If you want to add the share button to share whatever is on the card just add an ImageView in the card layout and to the ImageView add the onClickListener and add an Intent to be deployed.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);

You can also add an setType function which can be set to share a link.

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
  • Have you requested for storage permissions? Because to share any data you need storage permissions which you can enable with a pop-up during runtime. – Akshansh Jain Jan 14 '17 at 11:00