Android Home Screen Widgets - Androhub

Widget Banner

Android Home Screen Widgets

App widgets can be thought of as a small window or controller for an Android app that can be embedded in another application (like the home screen). They can be very useful, allowing users to view or control an app without actually launching it. The great thing about widgets is that they can be updated automatically (after a time period), or in response to user action. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that is able to hold other App Widgets is called an App Widget host.

The Basics

To create an App Widget, you need the following:

  1. AppWidgetProviderInfo object : Describes the metadata for an App Widget, such as the App Widget’s layout, update frequency, and the AppWidgetProvider class. This should be defined in XML.
  2. AppWidgetProvider class implementation : Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. Through it, you will receive broadcasts when the App Widget is updated, enabled, disabled and deleted.
  3. View layout : Defines the initial layout for the App Widget, defined in XML.

Widget Size

Before Android 3.1 a widget always took a fixed amount of cells on the home screen. A cell is usually used to display the icon of one application. As a calculation rule you should define the size of the widget with the formula: ((Number of columns / rows) * 74) – 2. These are device independent pixels and the -2 is used to avoid rounding errors.

As of Android 3.1 a widget can be flexible in size, e.g., the user can make it larger or smaller. To enable this for widget, you can use the android:resizeMode=”horizontal|vertical” attribute in the XML configuration file for the widget.

Available View and Layouts

A widget is restricted in the View classes it can use. As layouts you can use the FrameLayout, LinearLayout and RelativeLayout classes. As views you can use AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar and TextView.

As of Android 3.0 more views are available: GridView, ListView, StackView, ViewFlipper and AdapterViewFlipper. These adapter views require that you define a collection view widget which is described later in this {textselfreference}.

The only interaction that is possible with the views of a widget is via an OnClickListener event. This OnClickListener can be registered on a widget and is triggered by the user.

AppWidgetProvider

Your BroadcastReceiver implementation typically extends the AppWidgetProvider class.

The AppWidgetProvider class implements the onReceive() method, extracts the required information and calls the following widget life cycle methods.

As you can add several instances of a widget to the home screen, you have life cycle methods which are called only for the first instance added / removed to the home screen and others which are called for every instance of your widget. Below is the table describing all methods :

MethodDescription
onEnabled()Called the first time an instance of your widget is added to the home screen.
onDisabled()Called once the last instance of your widget is removed from the home screen.
onUpdate()Called for every update of the widget. Contains the ids of appWidgetIds for which an update is needed. Note that this may be all of the AppWidget instances for this provider, or just a subset of them, as stated in the method’s JavaDoc. For example, if more than one widget is added to the home screen, only the last one changes (until reinstall).
onDeleted()Widget instance is removed from the home screen.

Example

In this tutorial, we are going to learn how to create a simple Android widget, that updates automatically every 30 minutes, or in response to the user tapping the update button on the widget. Our widget generates and displays a random text on every update.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. Let’s create a layout for our widget naming app_widget_layout.xml and add place the below code into it.

4. Now create new java class naming MyWidgetProvider.java that  extend AppWidgetProvider. AppWidgetProvider has methods that are called when the app widget is updated, deleted, enabled and disabled among others. For our implementation, we only override onUpdate(), because it is the method called whenever the widget is added to a host.

Home Screen Widget
Home Screen Widget

Resize Widget
Resize Widget

5. Now to provide AppWidgetProviderInfo metadata we need to create xml file that defines additional information, features and data related to the widget. Data such as minimum layout dimensions (width and height), if the widget should be available on the lock screen (Android 4.2 and above), how frequently the widget should be updated, among many others. We define an xml file, called app_widget_provider_info.xml under  res/xml folder.

Find Widget
Find Widget

6. The final step is to add the app widget to the AndroidManifest.xml file. Within the <application> </application> element tags, place the below lines.

7. Finally all done, now you can also make apps with Widgets.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *