Widgets Presentation

Widgets Presentation

Goals

  • Build a simple home screen widget

Home Screen Widgets

  • App Widgets
  • The word "widget" is also used as a generel term for Views
  • Home Screen Widget is a View that can be placed on the home screen
    • Sizes
    • Limited

Steps

  • Specify initial properties for the widget
  • Tell Android about your widget
  • Define the initial layout of your widget
  • Respond to a broadcast to create widget
  • React to events on the widget

Specify initial properties for the widget

  • Create new XML resource file in xml directory
    • Root element is appwidget-provider
    • Attributes describe the widget
  • Size
    • android:minWidth
    • android:minHeight
  • Preview image
    • android:previewImage as a drawable
  • Layout
    • android:initialLayout reference to other layout XML file

Define the initial layout of your widget

  • This is just an ordinary layout XML file
  • The layout and its views will be created in Android system, not your process
  • Limited to what kinds of views and customizations you can do

Tell Android about your widget

  • Create a receiver XML element in AndroidManifest.xml
    <receiver android:name=".MyWidget" android:label="My Widget Label">
      <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
      </intent-filter>
      <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/mywidget_specification" />
    </receiver>
    

Class that handles app widget events

  • Events are actually so called Broadcasts
  • Create new class for handling the widget
    • Class extends AppWidgetProvider
  • Implement onUpdate method
    • Event handler for the widget
    • Called when widget is added to home screen
    • Called when it is time to update the contents of the widget
    • Without it, the widget would always look the same and not respond to any interaction
    • Responsible for telling Android what RemoteViews to use for the widget

The Widget handler class

  • Example
    public class MyWidget extends AppWidgetProvider {
      @Override
      public void onUpdate(Context context, AppWidgetManager manager, int[] widgetIds) {
      }
    }
    

Remote Views

  • An app widget is defined by a RemoteViews object
    • You don't have access to the actual View objects
    • The RemoteViews is just a specification, actual objects are created in Android system outside of your process
  • This means
    • You can't use the ordinary set-methods for changing the views and their styles
    • You are limited to the types of changes supported by methods in the RemoteViews class
    • You can only respond to events using PendingIntents and the events supported by the RemoteViews class

Customize the widget in onUpdate

  • All updates mean that you must create a new RemoteViews object
    • You don't have access to the widget's actual Views so they can't be changed
    • A widget update always mean that the whole contents of the widget is replaced
  • Possibly choose different layouts depending on some value
  • Change text displayed in widget
  • Change other properties of views, like background color
  • Remove and drop widget after each new version to be sure latest code runs

Creating and setting the RemoteView

  • Example
    public class MyWidget extends AppWidgetProvider {
      @Override
      public void onUpdate(Context context, AppWidgetManager manager, int[] widgetIds) {
        RemoteView view = new RemoteView(context.getPackageName(), R.layout.my_widget);
        view.setTextViewText(R.id.text, "Some text to show in widget!");
        for (int i = 0; i < widgetIds.length; i++) {
          manager.updateAppWidget(widgetIds[i], view);
        }
      }
    }
    

Notifications

  • Displayed at top of Android screen
  • Drag down from top to reveal larger display of all notifications
  • Have concept of RemoteViews in common with app widgets
    • In it's simplest form, no layout XML is needed
  • Android handles
    • Display the notification
    • Small icon in top bar, larger icon in revealed view
    • Title and longer text in revealed view
    • Starting activity via intent, if requested, when notification clicked
    • Remove the notification when dismissed by user

Display a notification

  • Exemple
    Notification.Builder not = new Notification.Builder(context);
    not.setContentTitle(String.format("Yeah, now we're at %d", _count));
    not.setContentText("Longer text shown when revealed view displayed");
    not.setSmallIcon(R.drawable.ic_launcher);
    
    NotificationManager manager = (NotificationManager)context.getSystemService(Activity.NOTIFICATION_SERVICE);
    manager.notify(1234, not.build());