Tuesday 20 August 2013

App Wigets|Android

Hello Friends,

I am writing here something on Widgets in android. Please make sure that you have basic knowledge of widget before going through this post. I have created this post to make you clear about widget concept and how to implement in android through easy steps. Hope you will like it.

App Widgets
Widgets are nothing but views.

Ex. Text View, Button, Check Box, etc.
Similarly App Widgets are small views of application. They are put into other application. They run in another app i.e. widgets run in another process. This other applications are known as App Widget Host.  Ex, Home Screen.

Note: - You can create your own App Widget Host.

For creating App Widgets, You need 3 things.
1) XML LAYOUT
2) METADATA FILE
3) BROADCAST RECEIVER

XML LAYOUT Resource  :-  Initial Layout of App Widget. It is XML file.  It uses remote views. Remote views are used to define & update view hierarchy in another app process.

METADATA (AppWidgetProvider Info) :-   Metadata of widget.
Ex, what is Widget’s layout? , What is update frequency of widget, Which is App Widget Provider Class (widget’s broadcast receiver).
Metadata is defined in XML.

BROADCAST RECEIVER (AppWidgetProvider Class) :-  It is class that has basic method that allows you to programmatically interface with app widget i.e. to interact with widgets (remote views).
This Provider class basically broadcast receiver which receives broadcasts when app widget is updated, enabled, disabled, deleted. This broadcast receiver content intent filters to listen for broadcast intents requesting for widget updates.

App Widget Size Guide Lines:-  
Please refer this link for details. 
Each widget much defines minWidth & minHeight in metadata. (Minimum height & width for widget)
Home screen offers users grid of available space.
For n cells available size is 70 * n-30.

HOW TO MODIFY WIDGETS(VIEWS):-  To modify views that form  app widget create remote view , modify remote view , apply remote view using AppWidgetManager.
Which type of changes ex, changing view’s visibility, changing text, changing image of imageview.

Steps for modifying Remote Views

1)      Create RemoteView:
Remoteview views= new RemoteViews(context.getpackage(), R.id.widget_layout);
1st parameter: package of widget
2nd parameter : layout file  of widget.

2)      Modify Remoteview:
Views.setviewvisibilty(R.id.textview_widget,,view.unvisible);
Views.setTextViewText(R.id.textview_widget,”invisible”);

3)      Applying RemoteView changes:
to apply changes to widget , we must know its ID. But there may be widget  of same type running on home screen.
AppwidgetManager.updateAppWidget(appwidgetIds,views);
1st parameter: integer array of all appwidgetds
2nd parameter: remoteview   that we created.

There are two places where we can update views
1)      Updating in OnUpdate handler (quite easy)
Step 1: get list of all ids(one of the handlers parameter)

Step 2: iterate through each id.

Step 3: update views for each id using appwidgetmanager(one of the parameter of onupdate handler)

2)      Outside onUpdate i.e in service,activity,broadcast receiver
Step1: get AppWidgetManager Instance

AppWidgetManager apM= AppWidgetManager.getInstance(pass current context)

Step 2: get all ids

Componentname  thisWidget = new ComponentName(current_context,MyAppwidgetProvider class)
Appwidgetmanager.getIds(thisWidget);

Step 3: Updateviews using AppWidgetManager (apM).        

AppWidget inherits  all permission of parent process i.e.(Home Screen) so for security reason interaction with AppWidgets are following
1)Adding click listener to one or more views
2)changing UI Based on selection  
3) transitioning between views within a collection view widget.

Note: there is no technique to directly enter Text. If you need to input text , then add click listener that will display activity and enter in that activity.

How to open activity: onclick view listener,open activity but your view don’t have permission to open activity so pending Intent is the mechanism.

Views.setonClickPendingIntent(R.id.widget_text,pendingIntent)

For changing images on selection use selectors.

Refreshing widgets : your widget should be up to date, as it appears on home screen,
1) using minimum update rates(update rate is mentioned in metadata file)
 
2) using event driven model
In provider(broadcast receiver) register intent filter for all the events(broad casts) when u want to update your widget.


Thanks

Maulik Dhameliya

dhameliya blog