Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
When Google Play Services are available, handle the OnClickListener callback by sharing directly with Google+
Advanced UI API Options
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's handle the shareButton next.
0:01
When Google Play Services are available.
0:03
We're gonna wanna go ahead and
0:04
set up our shareButton to be visible and
have an OnClickListener.
0:07
And when we click it, we wanna go ahead
and try to share directly to Google Plus.
0:14
So to do that.
0:18
We actually have a really handy
builder part of Google play services.
0:19
It's called the plus share builder and
we can give it an activity.
0:25
And then we can set our content here.
0:30
So the first thing we're going
to do here is call set type.
0:32
So the type we have is
just some simple text.
0:35
And a set text method allows
us to put some text here.
0:39
I'm gonna say check out this item on
etsy and give it the title of the item.
0:43
So the listing has a title property.
0:47
And we can also say set content URL and
give it a URI.
0:50
And the listing also has a URL.
0:54
Lets make sure we import.
0:56
And finally we can call get intent,
to get that intent with the prebuilt data.
0:58
Now since we had the plus one button
1:03
calling through to start activity for
result.
1:06
Now we're gonna have activity.star
activity for result, and
1:08
we're gonna have this new intent as well.
1:13
We need to define some constants.
1:16
So, at the top of this class here
I've just defined two constants.
1:18
One for +1 and one for sharing.
1:22
So for the +1 I've gone ahead and
updated the initialize method
1:25
to include the REQUEST_CODE_PLUS_ONE
which is a unique integer.
1:30
And I'm gonna include
here REQUEST_CODE_SHARE.
1:34
Now we can differentiate
between those two, and
1:38
here in the main activity,
instead of always updating the adapter,
1:40
we're just gonna check if
the request code was our +1 code.
1:45
And if it was, then we'll go ahead and
update the adapter.
1:50
The other case where
Google Play Services is not available.
1:56
Still want to show that button and
have a click listener on it.
1:59
And in this case,
2:02
we just want to create a generic
Android intent, with some data on it.
2:04
So, using the plus one builder,
we are able to populate the url.
2:08
Which is going to create a post on
Google Plus with the url link there.
2:13
And that includes some meta information
like a picture and things like that.
2:17
For this, this is just gonna use
the generic intent system from Android.
2:21
It's a little bit different, less
specific to the app, but it still works.
2:25
Create an intent, and the action
we're gonna use is the action, send.
2:29
And we're just gonna put some data on it.
2:34
So, we're gonna put an extra.
2:37
And the extra is in the intent class,
it's text.
2:40
And we're just gonna put out the same
content that we were doing for
2:42
the Google Plus, but we would need
to include it all here in one.
2:48
So I'm going to include the URL
as part of this text here.
2:51
So, check out this item on Etsy.
2:54
And we want to have the listing title and
then space and the listing url.
2:58
And then we're gonna set the type
again to be just text plain.
3:05
And then for this, we're going to call,
activity for result,
3:15
but we're going to explicitly create.
3:19
A Chooser here.
3:23
So what this is gonna do is make
sure that it allows the user to
3:24
choose the app they want.
3:27
And give it a title to use here, Share.
3:30
And let's not forget we need to include
a request code along with this.
3:33
Okay, this looks really great.
3:41
Let's go ahead and run our app, and
check out to see if this was working.
3:43
Okay, our list of items has come up.
3:56
And we can see that Google Play Services
connected successfully because the +1
3:57
button is visible to us.
4:02
And let's go ahead and test it out.
4:05
You hit +1 here.
4:07
We can see that, welcome back,
I'm signed into my Google+ profile.
4:10
And that information is here,
and I can go ahead and +1 this.
4:14
Click OK.
4:18
And the plus one button has been updated.
4:20
When it called back to on activity result,
I called back to the listing adapter.
4:22
And called notify data set change.
4:28
So the view has updated
to the correct state.
4:30
Let's go ahead and
test out sharing this now.
4:32
I'm gonna go ahead and
share this with my Google Plus followers.
4:34
I just wanna share it publicly.
4:38
Then say next and go ahead and share it.
4:40
That's gonna go out and
send it through the Google+ APIs.
4:43
And you can see that I don't have
the Google+ app, so the Google+ and
4:48
Google Play services library
handles this case for us.
4:52
So if the Google+ app is not installed.
4:56
We need to go ahead and install it.
4:59
If that's not the case,
we're just gonna say, not now.
5:01
We can't share right now.
5:03
That's okay.
That's because, on the emulator,
5:04
it has Google Play Services, but
it doesn't have the Plus app.
5:06
So on a real user's device, they'd be able
to go to the Play Store, download Google+,
5:09
and share directly there.
5:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up