Widgets are miniature applications that can be nested in other applications. The most common is nesting in the Home Screen application ie the desktop. We will make a simple widget that displays the value of the variable x, and each time the button clicks, it increments xo 1 and re-displays it.

Layout file

We create a new plain android project. The MainActivity activity and its associated layout file can be rewritten as you like. It will be displayed the first time you launch an application and can, for example, provide an information role. First, we add an xml file that specifies the layout of our widget. To the directory of the layout (which is a subdirectory of the res directory) we add an xml file. I named my layout_widgeta.xml

The contents of the file look like this:

Note that for widgets not all layouts and components are allowed. You can use only the following.

layouts:

FrameLayout

LinearLayout

GridLayout

RelativeLayout

components:

AnalogClock

button

chronometer

ImageButton

ImageView

ProgressBar

TextView

ViewFlipper

ListView

GridView

StackView

AdapterViewFlipper

The configuration file for the widget

In this file we define widget initialization settings, such as minimum width and height, refresh rate.

In the res directory we add an xml subdirectory and create a new XML file. I named my widget_provider.xml
Its contents look like this:

I have previously declared a layout file as a layout loaded at application startup, minimum widget width and height. The updatePeriodMillis parameter defines how often to be called (this is automatically) the onUpdate method of the derived class object after AppWidgetProvider, which is responsible for updating the widget.

The class responsible for updating the widget

We now create a new class that inherits from the AppWidgetProvider class. The most important is the implementation of the onUpdate method. This method is called in regular cycles (defined in us in the widget_provider file) and is used to update the widget. We can not hook components to the widget like normal components in normal applications. Ie. We do not attach them to class objects that extend the View class, and to RemoteViews. This is due to the fact that our method manages objects from another view (remote) and this is what we need to do with widgets. In line 21 we attach the layout of our widget. On line 22 we define event handling for the click of our button (defined in the layout_widgeta.xml file). Event handling does not, however, rely on the definition of a traditional listener with a direct action code,

Class that receives the intent

We also add a class that inherits the BroadcastRecever (nothing new, it has already appeared in previous lessons). This class is used to receive a call from the UpdateWidget class of intent. Receive and verify that the proper intent is in the onReceive method. We check, however, whether the intent that “came” is an intent of the system.Application.IKS_PLUS_PLUS, in case our application could be the recipient of any intentions (but it must be registered as the recipient of such intent in the manifest file).

Update manifest file

Now, in the manifest file, we need to include the necessary information about the widget support class, and the class responsible for handling the pl.jsystems.widget.options.IKS_PLUS_PLUS intent. The content of the manifest file after update: