Android Widget: Android - LeaVe my baThRoom at-least !

What is a widget?  In Android, the word widget is a generic term for a bit of self-contained code that displays a program, or a piece of a program, that is also (usually) a shortcut to a larger application. We see them every day on web pages, on our computer desktop and on our smartphones, but we never give too much thought into how great they are. Widgets first appeared in Android in version 1.5, and really gained traction thanks to HTC's Sense-flavored version of the operating system. Prior to the release of the HTC Hero and our first taste of HTC Sense, widgets were functional, but pretty bland in appearance. Since then, the people making our phones and independent developers alike have done some marvelous things with widgets, and it's hard to imagine using Android without them.
Android support to implement widgets for both, the home screen and the lock screen.

Common Types of Android Widget
Widget typically fall in one of the following categories

1. Information Widgets
Information widgets display information elements that are important to a user and track how that that information changes over time. Touching information widgets typically launches the associated app and opens a detail view of the widget information. Example for information widgets are weather widgets, clock widgets, etc.

2.Collection widgets
As the name implies, collection widgets specialize on displaying multitude elements of the same type, such as a collection of pictures from a gallery app, a collection of articles from a news app or a collection of emails/messages from a communication app. Collection widgets typically focus on two use cases: browsing the collection, and opening an element of the collection to its detail view for consumption. Collection widgets can scroll vertically.

3.Control widgets
The main purpose of a control widget is to display often used functions that the user can trigger right from the home screen without having to open the app first. A typical example of control widgets are music app widgets that allow the user to play, pause or skip music tracks from outside the actual music app. Power Control Widgets are using to make changes to the system settings easier and simpler. You just need to toggle On or Off almost all the system settings with just one tap.

4. Hybrid Widget
They combine the elements of different other widgets in one.

Some Widget Limitations
Only two gestures available for widgets are Touch and Vertical Swipe
Due to gestures limitations, some of the UI elements that rely on restricted gestures are not available for widgets.

To create your own widget and use your widget in layout XML, there are two additional files for you need to create. Here is a list of files you'll need to create to implement a custom widget:

XML Definition File - Click on your project and create a new folder called xml. Now right click on the newly created folder and create a new XML file. An XML flie defines the XML element used to instantiate your widget, and the attributes that it supports. The resource type of the XML file should be set to AppWidgetProvider. In the xml file, define some properties which are as follows :-

Layout XML [optional]- An optional XML file inside res/layout/ that describes the layout of your widget. You could also do this in code in your Java file.

Java Implementation File- This is the file that implements the behavior of the widget. If you can instantiate the object from layout XML, you will also have to code a constructor that retrieves all the attribute values from the layout XML file.

Java File must extend AppWidgetProvider class and override its update method. In the update method, you have to deifne the object of two classes PendingIntent and RemoteViews.
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
In the end you have to call an update method updateAppWidget() of the AppWidgetManager class.
Following are the other Methods of AppWidgetProvider class to manipulate widgets.
onDeleted(Context context, int[] appWidgetIds)
This is called when an instance of AppWidgetProvider is deleted.

onDisabled(Context context)
This is called when the last instance of AppWidgetProvider is deleted

onEnabled(Context context)
This is called when an instance of AppWidgetProvider is created.

onReceive(Context context, Intent intent)
It is used to dispatch calls to the various methods of the class

You also need to declare approvider widget class in Android manifest file as follows

<receiver android:name="ExampleAppWidgetProvider" >
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   <meta-data android:name="android.appwidget.provider"
      android:resource="@xml/example_appwidget_info" />
here is the example of application widget which create basic widget which will open the Web Browser.
Content of the


import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends AppWidgetProvider{
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
        for(int i=0; i < appWidgetIds.length; i++){
            int currentWidgetId = appWidgetIds[i];
            String url = "";

            Intent intent = new Intent(Intent.ACTION_VIEW);

            PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main);

            views.setOnClickPendingIntent(, pending);
            Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();
Content of the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
   xmlns:tools="" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:textSize="35dp" />
      android:text="Visit SCTPL"
      android:layout_below="@+id/textView" />

Content of the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
   package="" >

      android:theme="@style/AppTheme" >
      <receiver android:name=".MainActivity">
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
      <meta-data android:name="android.appwidget.provider"
content of the res/xml/widget.xml
<?xml version="1.0" encoding="utf-8"?>
Install App and Go to your widget section and add your created widget to the desktop or home screen.
Android widget application output

Popular posts from this blog

MongoDB - daTa ModeLLinG Concepts: SQL - Where all to UsE ?

Comparing Bootstrap With Google's Material Design Lite : Bootstrap - ResponSive Design

MongoDB overView with Installation_procEss: SQL - Where all to UsE ?