Home   →   Blog   →   Android Widgets

Android Widgets

Android Widgets are small applications that can be embedded in other applications such as a home screen or a lock screen and receive periodic updates. These elements run as a part of a process of a main application, but don't require running the main application explicitly.

Widget use RemoteViews object to provide self user interface. Class of this object describes a view hierarchy that can be displayed in another process. RemoteViews imposes some restrictions on the usage of view and layout types. You can’t use custom elements in widget, but if you still want to use a custom font, for example, then you have to translate it into a ImageView and send to the RemoteViews object. The list of supported views and layouts can be found on the AppWidgets on Google Developer Portal

There can be widgets of any size, but Google recommends a formula to calculate the widget size: ((Number of columns / rows) * 74) ­ 2.

To create a widget you need to implement layout xml file. Secondly, you need to create a class that will extend a class AppWidgetProvide and contains the life cycle callbacks of widgets.

public class MyWidgetProvider extends AppWidgetProvider {
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  final int N = appWidgetIds.length;
   for (int i=0; i<N; i++) {
    int appWidgetId = appWidgetIds[i];
    Intent intent = new Intent(context, ExampleActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
    views.setOnClickPendingIntent(R.id.image, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);

onUpdate method will be called depending on the update period, it is necessary to perform actions that change the state of the widget. Also in the manifest of your application you must define the widget, in order to do this add the receiver and intent ­filter, for example:

<?xml version="1.0" encoding="utf­8"?>
<appwidget­provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:widgetCategory="keyguard|home_screen" />

Google recommends to set up at least one hour of the minimum period of update of your Android mobile application widget(s) so you could save the battery level. If you want to avoid this limitation and update your widget more often, you'll have to use an AlarmManager and additionally set pdatePeriodMillis parameter to ”0”.

private void startRepeating(Context context) {
 final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Calendar calendar = Calendar.getInstance();
  alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), UPDATE_PERIOD, serviceIntent);

private void cancelRepeating(Context context) {
  final AlarmManager alarmManager = (AlarmManager)
  if (serviceIntent != null) {

Using of AlarmManager supposes to move updates logic of the widget to Service or IntentService.

public class UpdateWidgetService extends IntentService {
  private AppWidgetManager appWidgetManager;
  private RemoteViews remoteViews;
  public UpdateWidgetService() {

  protected void onHandleIntent(Intent intent) {
   appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
   int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)
   for (int widgetId : allWidgetIds) {
    // TODO: refresh logic.

Android widgets are a great way to show off its customization flexibility, allowing a home screen to be made as functional as possible and be tailored to the user's needs.

As usually we are waiting for your comments :)

comments powered by Disqus
Cf68cdaaf75f9cd1417267c6b1cafb3813c488a1 Virtual Reality Applications
Augmented reality apps are widely used – from geolocalization to using it in gaming industry. Augmented reality app is a new way to access data determined by environment conditions.
754d17aa02f8c56a0e0b671d45bf2455f6177062 Agile Methodology (Scrum)
Scrum is the very popular agile methodology for managing product development and project delivery. Scrum is the framework that could be applied for the development of product use cases, different projects or entire products.
1a1b621bfd24aba09a56bb25cf18719ad46ef190 News App Development
Smartphones are regularly used everywhere & anytime: in cars, on the way to the office, during the rest on holidays or weekends. That’s very convenient way for users to access important or just fun information with the help of news mobile app.
D63ac21dfd17c4acc93d52d9bdc72fea10e84edd Geolocation Services in Mobile Tracking Software
Geolocation services are used by many mobile apps, websites, mobile operators etc.