This workshop will be retired on May 1, 2025.
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
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll see how we can update the widget to receive clicks!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Eric Beasley
7,822 Points0 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We just finished putting together all
the pieces that make up a widget but
0:00
it doesn't do very much.
0:04
Let's see if we can't spice
things up a bit by making it so
0:05
that when we tap on the widget,
it randomly changes color.
0:08
Normally, we would be able to do this by
adding an OnClickListener to our frame
0:11
layout.
0:16
But since our views are hiding
inside a remote views object,
0:16
we're not going to be able to do that.
0:20
Instead, what we can do,
0:22
is set a pending intent which we will fire
off when we click on our frame layout.
0:24
And we're going to make that pending
intent trigger a call to onUpdate,
0:29
where we'll update
the color of our widget.
0:33
Let's start in our widget provider and
add some space between where
0:36
we declare our remote views and
where we update our widget.
0:40
Then we need to create a pending intent
but in order to create a pending intent,
0:44
we must first create a regular intent.
0:49
So let's create a new intent,
named intent.
0:51
And let's pass in our context for
the first parameter, and for the class,
0:55
let's pass in our widget provider.
1:00
Then on the next line,
let's call intent.setAction and
1:02
pass in
AppWidgetManager.ACTION_APPWIDGET_UPDATE.
1:09
Then let's add another line and
1:15
let's add our app widget IDs
as an extra to this intent.
1:18
Intent.putExtra and for
the name, let's pass in
1:22
appWidgetManager.EXTRA_APPWIDGET_IDS and
1:27
let's finish up by passing in
appWidgetIds for the value.
1:32
Now that we've got our intent,
1:38
we just need to use this intent
to create a pending intent.
1:39
Let's add two lines and then create a new
variable to hold our pending intent and
1:43
let's set it = PendingIntent.getBroadcast,
which takes in four parameters.
1:50
I'm going to do these each on
a separate line to keep things visible.
1:57
The first parameter is the context,
which we've already got access to.
2:01
Next, we've got the request code,
the request code is just a way for
2:06
us to differentiate between
two similar pending intents.
2:10
Pending intents get stored
in the Android system, and
2:13
they're not stored individually.
2:17
If there's an existing pending intent
that looks just like yours does,
2:19
Android's not going to create another one.
2:23
But if we'd like Android to
create another pending intent,
2:25
we can pass an unique request code to make
Android think that this one is different.
2:29
Also, if you want to know if Android
thinks two pending intent's are the same,
2:34
just use the intent.filter= method.
2:39
That said,
we're fine just passing in zero.
2:41
Next, we need to pass in our intent,
and finally,
2:45
we need to add any flags to specify
how to handle our pending intent.
2:48
Let's pass in
PendingIntent.FLAG_UPDATE_CURRENT.
2:53
This way, if our pending intent
already exists, then we'll keep it and
3:00
we'll update its extra data with
the new data in this version.
3:05
There's a few other options for flags and
if you'd like to read more about them,
3:09
there's a link below in
the teacher's notes.
3:13
All right, last but not least,
3:15
we need to attach this pending intent
to the frame layout in our widget.
3:17
To do this we can just use the set
on click pending intent method
3:21
on a remote use object.
3:26
So on the next line let's type
removeViews.setOnClickPendingIntent and
3:28
then for the view ID let's pass in
our frame layout R.id.frameLayout and
3:35
then let's pass in our pendingIntent for
the pending intent.
3:41
And now,
whenever we tap on one of our widgets,
3:47
it will trigger a call to this
onUpdate method, awesome.
3:50
But we still haven't done anything
to make it change color and
3:54
we'll get to that in the next video.
3:57
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