How to create a widget?

Asked

Viewed 2,235 times

2

I have in mind the @xml/widget_info the receiver in the Manifesto the layout of the widget and the Provider class is what I doubt. What it needs to have?

1 answer

3

To create a widget on Android you need to:

  • A layout for the widget, i.e. a common layout file, which will be in the res/layout folder
  • An xml describing widget properties, i.e a Appwidgetproviderinfo
  • A class of the kind Broadcastreceiver to build widget interface
  • Describe the widget in your AndroidManifest.xml
  • And opitionally you can use a Activity to configure something you want when the user adds its widget to the main screen for the first time.

I learned to make widgets by following this tutorial from Vogel, and I used this same article as the basis to answer here for you.

And by answering your questions about widget_info and the receiver, widget_info needs to define the layout the widget will use, the width and height of the widget, and the time rate at which your widget will receive updates, i.e. how much time your receiver will be called. using the example of Vogel, follow the example of widget_info:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="300000" >
</appwidget-provider> 

and on the receiver, its class that extends the Appwidgetprovider will have a method called onUpdate, this method will be called during the interval that Voce defined in widget_info and in it you can make updates in the widget, such as exchanging an image or text, or any type of update you want to put in the widget.

  • I’ve followed Vogella but no success ;/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.