Skip to content

Instantly share code, notes, and snippets.

@mariol3
Created November 29, 2015 13:11
Show Gist options
  • Save mariol3/d43b0629756b8227e037 to your computer and use it in GitHub Desktop.
Save mariol3/d43b0629756b8227e037 to your computer and use it in GitHub Desktop.
ShareActionProvider in Android Fragment. Project created in Google's Udacity course on Android Development: https://www.udacity.com/course/developing-android-apps--ud853
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_share"
android:title="@string/action_share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
package it.mariolization.android.sunshine;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailActivityFragment extends Fragment {
private static final String LOG_TAG = DetailActivityFragment.class.getSimpleName();
private ShareActionProvider mShareActionProvider;
private String forecast;
private Intent mShareIntent;
public DetailActivityFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_fragment_menu, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(mShareIntent);
}
else {
Log.d(LOG_TAG, "Share action provider is null");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent();
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
forecast = intent.getStringExtra(Intent.EXTRA_TEXT);
TextView forecastTextview = (TextView) rootView.findViewById(R.id.detail_forecast_textview);
forecastTextview.setText(forecast);
setupShareIntent();
}
return rootView;
}
private void setupShareIntent() {
String textToShare = forecast + " #SunshineApp";
mShareIntent = new Intent(Intent.ACTION_SEND);
mShareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
mShareIntent.setType("text/plain");
mShareIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="it.mariolization.android.sunshine.DetailActivityFragment"
tools:showIn="@layout/activity_detail">
<TextView
android:id="@+id/detail_forecast_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment