Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll add some meta-data and finally see the widget!
-
0:00
We just left off with needing to update our manifest
-
0:03
to include our app WidgetProvider.
-
0:05
So let's head over to a manifest and get to work.
-
0:08
Inside the application tags, let's add a new receiver element and
-
0:12
specify WidgetProvider as the name, receiver and widgetProvider.
-
0:18
Then let's close the starting tag and
-
0:20
next inside the receiver element we need to add an intent filter.
-
0:25
And inside the intent filter, we need to add an action
-
0:31
with the name android.appwidget.action.,
-
0:36
in all caps, APPWIDGET_UPDATE.
-
0:40
You can just copy and paste it in from the teacher's notes.
-
0:43
The purpose of this intent filter is to make sure that our
-
0:45
app WidgetProvider will receive it's update events.
-
0:49
All the other events are sent automatically, so
-
0:51
this is the only one that we need to specify.
-
0:54
Next to finish out our manifest entry, we need to add a metadata element
-
0:58
pointing to the resource that contains this widget's information.
-
1:02
So, under the intent filter, let's add a new metadata element, and
-
1:07
then let's add a name attribute, and let's set this = android.appwidget.provider.
-
1:16
Next, we need to specify the Android:resource resource, so
-
1:21
let's add that in there, and then let's leave it as an empty string for now.
-
1:25
And that's all we need from our metadata element, so let's close that out.
-
1:29
To finish off our widget, we just need to provide an app WidgetProvider info object
-
1:33
as the resource here.
-
1:34
An app WidgetProvider info object just specifies some basic features
-
1:39
of our widget, like how frequently it will update, and what size it should start as.
-
1:44
Luckily, Android's got an easy way for us to create one of these in XML.
-
1:49
Over in the project pane, let's start by creating a new resource directory for
-
1:54
XML files.
-
1:57
New, Android resource directory, and as the resource type,
-
2:01
click xml, and then hit OK.
-
2:04
Then, let's add a new XML resource file to our new directory, and
-
2:11
name it widget_info.
-
2:14
Next, since this isn't going to be a preference screen,
-
2:18
let's change this element to be appwidget-provider.
-
2:22
But before we move on, I want you to know that this is still an app WidgetProvider
-
2:27
info object, not an appwidget-provider as the tag would lead you to believe.
-
2:32
Also, let's get rid of the ending tag as we won't be needing it.
-
2:38
Okay, so inside our appwidget-provider info,
-
2:41
there's a few attributes about our widget that we need to specify.
-
2:44
The first of these are the minWidth and minHeight attributes,
-
2:48
these are basically just the default width and height of a widget.
-
2:52
But since widgets are placed on a grid, we can't always get exactly the size we want.
-
2:57
So we just set a minimum and Android makes sure that we don't go smaller than that.
-
3:01
Let's set our minWidth and minHeight to 50dp.
-
3:06
Next, let's specify the updatePeriodMillis attribute,
-
3:11
and give it a value of 3 million and 600,000.
-
3:15
This specifies how frequently our appwidget-provider will
-
3:20
receive calls to on update.
-
3:22
So, right now on update would be triggered about once an hour.
-
3:27
Also, note that the fastest this is allowed to go is once every 30 minutes.
-
3:32
The next attribute we need to specify is initial layout and
-
3:38
we just need to set it equal to the layout for our widget, so @layout/widget.
-
3:45
Next comes the resize mode,
-
3:48
here we specify how a user will be allowed to resize the widget.
-
3:52
We're going to let users resize the widget both horizontally and vertically.
-
3:56
So let's set this equal to horizontal, and then a vertical pipe, and then vertical.
-
4:02
Finally, we need to specify the widget category, and
-
4:06
since we're making a home screen widget, let's just pick home_screen.
-
4:10
Now that we've conquered our widget info, let's head back to our manifest and
-
4:15
plug this in for the resource at xml/widget_info.
-
4:22
We're all ready to run our app and see if the widget works but
-
4:25
before we do that, let's make a quick change to our run debug configuration.
-
4:30
Since we don't have an activity, we shouldn't be trying to launch an activity.
-
4:35
So let's change Launch options from Launch, Default Activity,
-
4:40
to Launch Nothing, then let's hit OK and run the app.
-
4:47
Once the app is installed, long press on a space on your home screen,
-
4:52
pick the widgets option and
-
4:54
then scroll down to list widget and then add it to your home screen.
-
5:03
And there it is, a beautiful blue square and if we long press on it,
-
5:08
we can resize it too, awesome.
-
5:13
We've covered a ton about widgets, and
-
5:15
we even got a working widget at this point but there's still a bunch more to cover.
-
5:20
In the next video, we'll add some interactions to our widget.
You need to sign up for Treehouse in order to download course files.
Sign up