Android Tutorial - Clickable Widgets


Another quick Android tutorial. I couldn't find an easy or correct method of launching a browser when you click on a homescreen widget. Well, here it is...

public class clickWidget extends AppWidgetProvider
{
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
RemoteViews remoteViews =
   new RemoteViews( context.getPackageName(), R.layout.widget );
remoteViews.setImageViewResource(R.id.ImageView01, drawableResourse);

ComponentName myWidget =
   new ComponentName( context, clickWidget.class );

// Create an Intent to launch Browser
Intent intent =
   new Intent(
      Intent.ACTION_VIEW, Uri.parse("http://example.com")
   );
PendingIntent pendingIntent =
   PendingIntent.getActivity(context, 0, intent, 0);

remoteViews.setOnClickPendingIntent(R.id.ImageView01, pendingIntent);
appWidgetManager.updateAppWidget( myWidget, remoteViews);
}

I've used this as the basis of a demo widget - "MI5 Terror Threat Level". The widget displays the UK's Threat Level on your homescreen. Clicking on it takes you to the MI5 page discussing the threat level.

The threat level is determined by parsing the RSS that the security services so helpfully provide. At the moment, the widget keeps a local copy of the graphics because the RSS feed contains references to "localhost" images.

You can download the widget by scanning in this QR code.

MI5 Widget - QR Code

MI5 Widget - QR Code


Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

One thought on “Android Tutorial - Clickable Widgets”

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">