Android App Widgets
Android widgets are views that can be placed on the home screen and are updated periodically. They could be a great options for frequent user interactions with your application without opening the application. They are basically the broad cast receivers which display some data and interacts with the users.![]() |
| Resizable Android App Widget - Android Tech Point |
Steps to create the widget
Creating android widgets is a simple task. You only need to follow simple steps:- Create the xml layout file.
- Define the XML file (i.e AppWidgetProviderInfo) which includes properties for the widget like size, updating interval, minimum size, resizability etc.
- Create the java file which will have the callback methods for the app widget.
- Define the receiver for the widget in the AndroidManifest.xml.
- (Optional) Configuration activity which is called when a new instance of the app widget is made.
Available view in Android App Widgets
Widgets has some limitations. You can only use few views in the xml of widgets. They are as follows:
AnalogClock- Button
ChronometerImageButtonImageViewProgressBarTextViewViewFlipperListViewGridViewStackViewAdapterViewFlipper
And the layout which are supported by the remote views are:
The subclasses of these views are not supported.
Creating Android App Widgets
Now let's dive into the code to create the widgets. You can get the complete project at Github Repository. Or you can get the code below in this post. We will follow the steps defined in the above portion.
- Create the xml layout file.
Here we will define the layout for our widget just like the regular android xml layout. - Define the XML file (i.e AppWidgetProviderInfo) which includes properties for the widget like size, updating interval, minimum size, resizability etc.
- Create the java file which will have the callback methods for the app widget. In some cases onAppWidgetOptionsChanged() is not called on the creation of app widget as it should according to offical docs, so you need to set the UI of widget in onUpdate() to be on safer side.
- Define the receiver for the widget in the AndroidManifest.xml. You need to define the receiver inside the activity tag. Also give the meta tag.
