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
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
Gestures
Only two gestures available for widgets are Touch and Vertical Swipe
Elements
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.
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
Example
here is the example of application widget which create basic widget which will open the Web Browser.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 widgetsAs 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
Gestures
Only two gestures available for widgets are Touch and Vertical Swipe
Elements
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.
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
Example
Content of the MainActivity.java
Content of the activity_main.xml
Content of the AndroidManifest.xml
content of the res/xml/widget.xml
Install App and Go to your widget section and add your created widget to the desktop or home screen.
Comments
Post a Comment