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