Build Android (Home screen) Widgets with AppWidgetProvider

Tram Ho

1. Introducing Home screen widgets and AppWidgetProvider

1.1 Home screen widget

  • The home screen widget is the broadcast receivers, which provide the components for interacting with the app. It is mainly used on the home screen of the Android operating system, as shown above. It will usually display a critical piece of app data and allow the user to interact. For example weather forecast app, it will display summary information but necessary for the user, when the user clicks on the widget, it will open the app.
  • Widget will have some weaknesses, because it is an application so it will take up RAM, the more widgets on the homescreen, the less RAM you will have to use. Or there are widgets that need to connect to a separate server then you will lose traffic. So, please consider when using widgets offline!
  • Widgets use RemoteView to create the interface, a RemoteView can be executed by another process similar to the root of an application. In this way the widget runs with its application’s permissions.
  • The UI for a Widget is defined by a broadcast receiver. This receiver attaches its layout to an object of type RemoteView. This object will be sent to Android, it will be moved to the home screen.

1.2 AppWidgetProvider

The AppWidgetProvider is a class extended from BroadcastReceiver, convenient for handling App Widget broadcasts. AppWidgetProvider only accepts event broadcasts that are related to App Widget, for example when App Widget is updated, deleted, enabled or disabled. When there are broadcast even, AppWidgetProvider accepts the following methods:

  • onUpdate () : This function is called to update the App Widget for a period defined by the updatePeriodMillis property in the AppWidgetProviderInfo. This method is also called when the user adds the App Widget, so it will do the necessary setup such as defining the event handlers for the Views and Start Service if needed. However, if you declare a configuaration Activity, this method is not called when the user adds the App Widget, but will be called for the next update.
  • onAppWidgetOptionsChanged () : This function is used since API 16 and up, it is called when it is first set home and it is also called when it is resized. You can use this callback to show or hide content based on the size of the widget. To get the size of the widget, calling getAppWidgetOptions () it will return a Bundle consisting of:
    • OPTION_APPWIDGET_MIN_WIDTH—: Contains the lower limit of current width, the unit is dp.
    • OPTION_APPWIDGET_MIN_HEIGHT—: Contains the lower bound of current height, in dp.
    • OPTION_APPWIDGET_MAX_WIDTH—: Contains the upper limit of current width, the unit is dp.
    • OPTION_APPWIDGET_MAX_HEIGHT—: Contains the upper bound of current height, in dp.
  • onDeleted (Context, int []) : This function is called when an APP Widget is deleted from the App Widget host.
  • onEnabled (Context) : This function is called when an App Widget instance is first created. For example, if a user adds two App Widget instances, it is only called the first time. If you need to open a new database or do a one-time setup for all App Widget instances then this function is suitable for you to do that.
  • onDisabled (Context) : This function is called when the last instance of the App Widget is removed from the host App Widget. This function is suitable for clean work when done in onEnable (Context) function, such as deleting temporary database.
  • onReceive (Context, Intent) : This function is called on broadcasts and before each of the above methods. You usually don’t need to implement this method because by default AppWidgetProvider it will filter all App Widget broadcasts and call the above methods.

Of the above callbacks, onUpdate () is the most important because it is called when the App Widget is added to the host. If your App Widget responds to user interactions then you should implement this method to handle it here. If the App Widget doesn’t need to create temporary files or temporary databases, or perform tasks that require a clean-up, you can simply call each onUpdate () function.

2. Create a home screen widget

To create a widget, you need:

  • Create a layout file
  • Creates an XML file (AppWidgetInfo) that describes the widget’s properties such as size or update cycle.
  • Create an extend class from AppWidgetProvider used to create the widget interface.
  • Install Widget information to AndroidManifest.xml file.

Before Android 3.1, a widget that was always hard-fixed was a fixed number of tiles on the home screen.

After that version, the widget can change size depending on the user’s intent. To enable this feature for the widget, you need to use this property in the xml file: android: resizeMode = “horizontal | vertical |

The first is to create a background file for the widget like this: background_widget.xml

Create layout file for widget: widget_layout.xml

File info for widget: created in directory res / xml: widget_info.xml

Then create the MovieWidgetProvider Class extend from the AppWidgetProvider:

Add widget information to AndroidManifest.xml file:

Here, I combine with FirebaseMessagingService to receive notifications and update widgets every time there is silent push:

(To receive silent push, the message will not fire, there is no notification device but only data)

The following results :

All out, goodbye and see you again !!

Refer :

Android docs:

Share the news now

Source : Viblo