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
Well done!
You have completed Android Widgets!
You have completed Android Widgets!
Preview
In this video we'll update our list with its adapter!
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
We've just finished setting up our
RemoteViews factory, which remember,
0:00
is just an adapter for remote use.
0:04
That's also why we decided
to call it WidgetAdapter.
0:06
But now we need to hook up our
WidgetAdapter to our widget.
0:09
And since both of these things are dealing
with RemoteViews that will be living
0:13
inside another application, Android's got
a special way that we need to handle this.
0:16
Basically, we need to get an instance of
our WidgetAdapter from within someone
0:21
else's application,
which really complicates things.
0:26
I mean, sure, the other application could
just instantiate our widget after but
0:30
then it would be created
with the wrong context.
0:35
So we wouldn't be able to do much with it.
0:37
To solve this problem,
0:40
we're going to create a service that
can return a new WidgetAdapter.
0:41
Then, instead of directly setting
our WidgetAdapter on our widget,
0:45
we can just use an intent
to point to our service.
0:49
This way, our widget can get its adapter
even from within another application.
0:53
It will just use that intent
to start up our service and
0:58
retrieve that WidgetAdapter.
1:01
Back in the code,
let's create a new class for
1:03
our service that will
return a WidgetAdapter.
1:05
And let's name it WidgetService.
1:09
Then, Android's gone ahead and
created a specialized service just for
1:12
this occasion.
1:16
So let's just extend
the RemoteViewsService class.
1:17
And then use Alt+Enter to
implement the one required method.
1:22
And now all we need to do is return
a new instance of our WidgetAdapter.
1:26
So instead of returning null,
let's return new WidgetAdapter.
1:31
And for the context,
we can just pass in this.
1:36
Last but not least,
since this is a service,
1:39
we're going to need to
declare it in our manifest.
1:42
Let's add some space below
our last receiver tag, and
1:45
then add a new entry for our service.
1:49
And accept WidgetService for the name.
1:52
Next, to prevent other apps from being
able to access our widget's data,
1:54
we need to add
the android:permission attribute and
2:01
set it equal to
"android.permission.BIND_REMOTEVIEWS",
2:05
which is also below in
the teacher's notes.
2:11
Then let's close our service tag.
2:16
And now we're finally ready to
add our adapter to our widget.
2:18
Over in WidgetProvider let's
2:21
add a few lines below where we
initialize our RemoteViews.
2:23
And then let's create a new intent
variable pointing to our service.
2:28
Intent, and
we'll call it serviceIntent = New intent.
2:32
And pass in a context.
2:40
And for the class, we wanna pass in our
service class, Widgetservice.class.
2:42
On the next line,
let's add our adapter to our list view by
2:49
typing remoteViews.setRemoteAdapter.
2:54
And then for the viewId, let's pass in
the idea of our list view R.id.listView.
2:57
And then let's pass along
our serviceIntent as well.
3:04
And now, if we run the app again,
we can see that we've got our list.
3:07
Though, let me make these a little bigger,
so they are easier to see.
3:12
And if we click on them, we can click on
the buttons and scroll them up and down.
3:23
But it looks like we've lost
the ability to change color.
3:30
In the next video,
we'll find out where the color went and
3:33
learn about how we can
interact with our list widget.
3:36
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