How to create a widget?

2

I have in mind the provider on @xml/widget_info o receiver on Manifest the layout of the widget and the class Provider is that I have doubt. What does it need to have?

    
asked by anonymous 20.02.2014 / 01:13

1 answer

3

To create a widget on Android you need:

  • 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 type BroadcastReceiver to build the widget interface
  • Describe the widget on your AndroidManifest.xml
  • And optionally you can use a Activity to set up something you want when the user adds your widget to the main screen for the first time.

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

And answering your questions about the widget_info and receiver, widget_info needs to define the layout the widget used, the width and height of the widget, and the length of time your widget received updates, ie how long your receiver will be called. using the vogel example, follow the widget_info example:

<?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 about receiver , your class that extends the AppWidgetProvider will have one method called onUpdate , this method will be called during the interval you defined in the widget_info and in it you can make updates to the widget, such as swapping an image or text, or any type of update you want to place in the widget. p>     

23.02.2014 / 00:49