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 learn about the AppWidgetProvider!
Related Links
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
By the end of this course, we'll have
a widget that displays a list and
0:00
lets us interact with that
list right from the widget.
0:04
But we're not ready to create
a list widget quite yet.
0:07
So we're going to start by creating
a widget to just display a certain color.
0:10
As always,
the first step is to create an app.
0:14
So let's start a new
Android Studio project and
0:17
name it ListWidget Then lets hit Next,
0:19
and Next again, and
then pick Add No Activity, and hit Finish.
0:24
Right now, our only goal is to create
a widget that displays a color, and
0:30
we don't need an activity for that.
0:35
So instead of starting with the activity,
0:37
let's start by creating the layout for
our widget.
0:39
But first, it looks like we need
to create the layout directory.
0:42
So let's right-click on res > New
> Android resource directory,
0:46
and then choose layout for
the resource type, and hit OK.
0:52
Now that we've got that, let's create
a new layout resource file named widget.
0:58
And let's make its root
element a FrameLayout.
1:07
Inside our layout, if we want to be
able to set the background color,
1:12
then we need to give
our FrameLayout an ID.
1:16
I'll call mine FrameLayout.
1:19
So android:id, And then frameLayout.
1:22
Next, let's give our FrameLayout
a starting background color.
1:29
android:background and I'll pick
1:34
@android:color/holo_blue_dark.
1:38
After we've got our layout, the next piece
that we need is the AppWidgetProvider.
1:43
The AppWidgetProvider is just
a specialized broadcast receiver.
1:48
It receives an intent and
the on receive function.
1:52
And then based on
the content of that intent,
1:54
it can call one of five helpful methods.
1:57
The first of these methods is onUpdate.
2:00
onUpdate is called periodically
to update your widget.
2:02
This is also called when
a user first adds a widget.
2:06
So you should give any set up for
your widget any onUpdate method.
2:09
Typically, onUpdate is the only
method you'll need to implement.
2:13
But for more complicated widgets, the rest
of these methods might come in handy.
2:16
onAppWidgetOptionsChanges is triggered
anytime a widget is resized,
2:20
including when it's first
placed on the screen.
2:25
This method lets us update our widgets
content based on the size of the widget.
2:28
onDeleted is triggered
whenever a widget is removed.
2:32
If there's anything specific to one
single instance of your widget,
2:35
you would clean that up here.
2:39
These last two methods
are a little different.
2:41
Instead of dealing with just one widget,
2:43
these methods are about all
the instances of your widget.
2:45
onEnabled is triggered the first time
an instance of your widget is added to
2:48
the screen.
2:52
If there's already an instance
of your widget on the screen,
2:53
this method will not be triggered.
2:56
Conversely, onDisable
is called when the very
2:58
last instance of your widget is removed.
3:01
If you were running a service
to update your widgets,
3:04
this would be a good place to stop it.
3:07
Now that we know a little more about
app widget providers, let's make one.
3:08
Let's create a new class, and
let's name it WidgetProvider.
3:13
And let's make it extends
AppWidgetProvider.
3:20
Then, since we're starting off simple,
let's use control+O and
3:25
only override the onUpdate method.
3:30
And we don't need a call to super,
so let's delete that.
3:34
Now we need to set up our widget, but it's
not really clear how we should do that.
3:37
We've got a context, and
let me minimize this real quick.
3:42
And an appWidgetManager,
and a list of appWidgetIds.
3:45
Well, we know what a context is, and
an appWidgetManager is just a part
3:50
of the Android system that lets
us communicate with our widgets,
3:53
even though they live
in other applications.
3:56
It's just a middleman between us and
our widgets.
3:59
The third parameter, appWidgetIds,
4:02
is a list of IDs associated with
the instances of our widget.
4:05
Each time we add a widget to the screen,
it gets a new ID.
4:10
And since users can add more
than one instance of a widget,
4:14
instead of passing in just one ID,
we get in an array of ID's.
4:17
So inside the onUpdate function, we need
to update all the instances of our widget.
4:21
Let's start by looping through
the appWidgetIds array for
4:27
(int id: appWidgetIds).
4:32
And then inside our loop let's
use our appWidgetManager and
4:37
call the updateAppWidget method.
4:41
Then let's pass in an appWidgetIds for
the first parameter.
4:44
And then we need to pass in
something called a RemoteViews.
4:47
Since our widgets end up
living in other apps,
4:50
they're not allowed to use
normal views like we're used to.
4:52
Instead, they use RemoteViews,
and while the RemoteViews is
4:56
fairly similar to a view, they're
somewhat limited in what they can show.
4:59
Check out the teachers notes below
to see what types of views and
5:04
layouts are allowed inside
a RemoteViews object.
5:07
But for now, we just need to create
a RemoteViews object which contains
5:10
the layout for our widget.
5:14
So let's add a line above
our update statement, and
5:15
then create a new RemoteViews object.
5:18
= new RemoteViews.
5:25
Then we just need to
pass in our package name,
5:29
which is context.getPackageName,
and then our widget's layout ID,
5:32
which is our R.id, or
rather R.layout.widget.
5:38
And once we've got our RemoteViews object,
we just need to pass it in down here.
5:43
Nice, that finishes up our
AppWidgetProvider for now, but
5:48
we still aren't quite able to use it.
5:51
Remember when I mentioned
the AppWidgetProvider
5:53
is a broadcaster receiver?.
5:56
Well as you know, for a broadcast
receiver to work, it needs to be declared
5:57
in our apps manifest, which we'll
take care of in the next video.
6:01
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