1

This app contains a recycler cardview ( 2 textviews ) .. how can i share the textview strings using a share button which i have created already..

MyRecycleViewAdapter.java

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>{

    private List<MyQuote> myQuoteList;
    private Context mCtx;

    public MyRecycleViewAdapter(List<MyQuote> list, Context mCtx) {
        this.myQuoteList = list;
        this.mCtx = mCtx;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sample_quotecards, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, final int position) {
        final MyQuote myQuote = myQuoteList.get(position);
        myholder.tv_author.setText(myQuote.getAuthor());
        myholder.tv_quote.setText(myQuote.getQuotedesc());
        myholder.im_favlike.setImageResource(R.drawable.fav_border);


        // share button of a recycler cardview
        myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


             //intent to share list values which are already defined to apps like facebook , whatsapp etc
ArrayList<String>testlist = new ArrayList<String>();
testlist.add(myQuoteList.get(position).getAuthor());
testlist.add(myQuoteList.get(position).getQuotedesc());

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testlist);
shareIntent.setType("image/*");
view.getContext().startActivity(Intent.createChooser(shareIntent , "share this quote via"));


            }
        });
    }







    @Override
    public int getItemCount() {
        return myQuoteList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_author;
        public ImageView im_favlike;
        public TextView tv_quote;
        public ImageButton buttonViewOption;

        public ViewHolder(View itemView) {
            super(itemView);
             im_favlike =(ImageView)itemView.findViewById(R.id.likeimg);
            tv_author= (TextView) itemView.findViewById(R.id.author_title);
            tv_quote= (TextView) itemView.findViewById(R.id.quote_text);
            buttonViewOption = (ImageButton) itemView.findViewById(R.id.imageViewOptions);
        }
    }
}

MyQuote.java

import android.os.Parcel;
import android.os.Parcelable;

public class MyQuote  implements Parcelable{

    private String author;
    private String quotedesc;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {

        parcel.writeString(quotedesc);
        parcel.writeString(author);
        }

    private MyQuote(Parcel parcel){

        quotedesc = parcel.readString();
        author = parcel.readString();

    }
    public static final Parcelable.Creator<MyQuote> CREATOR = new Parcelable.Creator<MyQuote>(){

        @Override
        public MyQuote createFromParcel(Parcel parcel) {
            return new MyQuote(parcel);

        }
        public MyQuote[] newArray(int size){
            return new MyQuote[size];
        }
    };

    //constructor initializing values
    public MyQuote(String author, String quotedesc) {
        this.quotedesc = quotedesc;
        this.author = author;
    }

    //getters
    public String getAuthor() {
        return author;
    }

    public String getQuotedesc() {
        return quotedesc;
    }
}

im getting the following error when i use intent like this.. Wrong 2nd argument type. Found: 'java.util.ArrayList', required: 'java.util.ArrayList' less... putParcelableArrayListExtra (String, java.util.ArrayList) in Intent cannot be applied to (String, java.util.ArrayList)

 public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, final int position) {
        final MyQuote myQuote = myQuoteList.get(position);
        myholder.tv_author.setText(myQuote.getAuthor());
        myholder.tv_quote.setText(myQuote.getQuotedesc());
        myholder.im_favlike.setImageResource(R.drawable.fav_border);


        // share button of a recycler cardview
        myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


             //intent to share list values which are already defined to apps like facebook , whatsapp etc
ArrayList<String>testlist = new ArrayList<String>();
testlist.add(myQuoteList.get(position).getAuthor());
testlist.add(myQuoteList.get(position).getQuotedesc());

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testlist);
shareIntent.setType("image/*");
view.getContext().startActivity(Intent.createChooser(shareIntent , "share quote via"));


            }
        });
    }
Manidev
  • 41
  • 1
  • 1
  • 8

1 Answers1

0

If you want to share a Simple date like a text with other applications have to use of Intents. For example with this code you can share your text with other applications:

Intent shareText = ShareCompat.IntentBuilder.from(activity)
  .setType("text/plain")
  .setText(shareText)
  .getIntent();
if (shareText.resolveActivity(getPackageManager()) != null) {
  startActivity(shareText);
}

But if you want to share a multipart data like an arrayList you can do that like this:

ArrayList<String> testList = new ArrayList<String>();
testList.add(yourFirstText); // Add your text here
testList.add(yourSecondText);// Add your text here

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share my list to.."));

More info can be access here

For your RecyclerView when the user clicks on each item of your list you can call this code that I wrote above.

Then you can create your ArrayList with items of that card. for example, in your onBindViewHolder you can set something like this:

myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

    ArrayList<String> testList = new ArrayList<String>();
    testList.add(myQuoteList.get(position).getAuthor); // Add your text here
    testList.add(myQuoteList.get(position).getQuotedesc);// Add your text here

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testList);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, "Share my list to.."));

                    }
                });

Hope this can helps you.

Ehsan
  • 2,676
  • 6
  • 29
  • 56
  • can you pls see this question ... https://stackoverflow.com/questions/52347734/passing-arraylist-value-to-a-intent-putextra-for-intent-action-send – Manidev Sep 16 '18 at 05:08
  • yes.. how can i share a particular list values to other apps in my case (quote and author ) – Manidev Sep 16 '18 at 05:21
  • thank you for your answer.. but as i'm new to android dev i'm little bit confused how to implement it.. – Manidev Sep 16 '18 at 06:23
  • You just need to replace your list with `testList` in my second example. – Ehsan Sep 16 '18 at 06:41
  • i hope you can help me ... if i explain the question in detail...cn you pls refer this question... here i explained what i want to do https://stackoverflow.com/questions/52352041/share-option-for-a-cardview-in-recycler-view – Manidev Sep 16 '18 at 07:51
  • my answer doesn't help? where is your problem? – Ehsan Sep 16 '18 at 07:53
  • I created quotes and authors like this in MainActivity list.add(new MyQuote("Nelson Mandela","It always seems impossible\n until it's done"));and so on...... but how to share particular quote n author when user cliks on share button of a specific card... See the demo here https://stackoverflow.com/questions/52352041/share-option-for-a-cardview-in-recycler-view – Manidev Sep 16 '18 at 08:20
  • check my answer again, – Ehsan Sep 16 '18 at 08:41
  • Wrong 2nd argument type. Found: 'java.util.ArrayList', required: 'java.util.ArrayList extends android.os.Parcelable>' less... putParcelableArrayListExtra (String, java.util.ArrayList extends android.os.Parcelable>) in Intent cannot be applied to (String, java.util.ArrayList) – Manidev Sep 16 '18 at 09:06