Skip to content

Commit

Permalink
added share button
Browse files Browse the repository at this point in the history
  • Loading branch information
Protino committed Jun 17, 2016
1 parent 23cf143 commit efb6049
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
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;
Expand All @@ -18,6 +23,7 @@ public class DetailActivityFragment extends Fragment {

public static String forecastData;
private String LOG_TAG = DetailActivityFragment.class.getSimpleName();
private ShareActionProvider myShareActionProvider;

public DetailActivityFragment() {
}
Expand All @@ -28,6 +34,42 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_detail, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

myShareActionProvider.setShareIntent(createShareIntent());
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

switch (id) {
case R.id.action_share:
break;
}
return super.onOptionsItemSelected(item);
}

private Intent createShareIntent() {
Intent myShareIntent = new Intent(Intent.ACTION_SEND);
myShareIntent.setType("text/plain");
myShareIntent.putExtra(Intent.EXTRA_TEXT, forecastData + "#Sunshine_App");
setShareIntent(myShareIntent);
return myShareIntent;
}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (myShareActionProvider != null) {
myShareActionProvider.setShareIntent(shareIntent);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

switch (id) {

case R.id.action_refresh:
updateWeather();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/menu/menu_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Activity.DetailActivity">

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
android:id="@+id/action_share"
android:title="@string/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<string name="pref_temperature_default" translatable="false">metric</string>
<string name="pref_temperature_key" translatable="false">temperature</string>
<string name="action_map">Map</string>
<string name="action_share">Share</string>


</resources>

0 comments on commit efb6049

Please sign in to comment.