Search code examples
androidandroid-intentsendparcelableshare-intent

Send multiple text items in ShareIntent?


My question is very similar to these ones:

Is it possible to share multiple text by using share intent?

Passing Multiple text/plain values in share intent

I've tried the suggested solutions and it hasn't worked. I'm trying to send two items: "name" and "arrival time" in share intent. The data from which the two texts(Strings) are downloaded into the activity via AsyncTask and RecyclerView is in the method below.

//initializing

  Schedule stationArrival;
            private String stationShareStationName;
            private String stationShareArrivalTime;


 @Override
    public void returnScheduleData(ArrayList<Schedule> simpleJsonScheduleData)
    {
        if (simpleJsonScheduleData.size() > 0) {
            scheduleAdapter = new ScheduleAdapter(simpleJsonScheduleData, StationScheduleActivity.this);
            scheduleArrayList = simpleJsonScheduleData;
            mScheduleRecyclerView.setAdapter(scheduleAdapter);
            scheduleAdapter.setScheduleList(scheduleArrayList);

            stationArrival = scheduleArrayList.get(0);

            stationShareStationName = stationArrival.getStationScheduleName();
            stationShareArrivalTime = stationArrival.getExpectedArrival();     
        }
       else
        {
            Toast.makeText(StationScheduleActivity.this, "Data currently unavailable", Toast.LENGTH_SHORT).show();
        }
        if (mShareActionProvider != null)
        {
            mShareActionProvider.setShareIntent(createShareIntent());
        }
    }

ShareIntent code. Currently it's wrong since only one of the items is displayed in the share screen. I've tried to put the two items into an ArrayList but it doesn't work since I have a separate method for the ShareIntent. Is there a way to do this w/o restructuring the current code? Thank you in advance.

public Intent createShareIntent()
    {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT,stationShareStationName);
        shareIntent.putExtra(Intent.EXTRA_TEXT,stationShareArrivalTime);
        return shareIntent;
    }

Solution

  • You can concat two stationShareStationName and stationShareArrivalTime and make single string. You can also use newline characters in between these variables ("\n") as below:

    public Intent createShareIntent()
    {
        String stationShareStationName ="xxx";
        String stationShareArrivalTime = "xxx";
        String data =stationShareStationName + "\n" + stationShareArrivalTime
    
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT,data);
        return shareIntent;
    }