Truly speaking,I got the idea of using update interval on appwidget following malubu’s blog. This blog entry about widget update interval, from my point of view is one of the best guide available on the web right now. So ,if I am going to write about update interval for my listview appwidget ,I will be basically writing the same stuff. So,referencing malubu’s blog will be the best where you can go and find out the concept behind setting update interval for any appwidget . Finally I want to thank Luong for this awesome tutorial.
Finally check out Luong’s github to access the entire source code

In this part of tutorial,I am going to explain on how to download images from server and show those images on Android AppWidget ListView's ImageView. As I stated earlier on my AppWidget ListView tutorial,it is better to use Database rather than static ArrayList. So ,here first of all I am going to implement a Database,where I will store all the data fetched from the web. Before going down to code, I will explain the logic that will be implemented in this tutorial.

First fetch the content of json file

Save the fetched content into database

Download images from web

Save the image Bitmap on file

Populate appwidget listview with data from Database and image from File

Let’s first create our Database which holds content,heading,image url of our remote json file. Further,this Database of ours will also hold the file path where our images will be saved.Let’s create SQListOpenHelper class which will define our Database and table structure. Let’s name it

The DatabaseHelper class is pretty self-explanatory to those who have already worked on Android SQLite. Moving on,let’s create a class which manages actions like adding,retrieving,updating database record and let’s name it

FileManager not only saves images on file but also updates the database record with the saved image path. Now we need to modify our RemoteService.java class to accommodate function like storing json content to database,downloading images and saving them to file. I will just point out the added portion on the code

All above procedure is done to save content on database and file. Now, instead of using static ArrayList as we had done previously,we will fetch the content from database and update or populate the appwidget listview with those contents.We need to modify our WidgetService.java and ListProvider.java classes a little. On our WidgetService class,we fetch the data from database and supply those data to our ListProvider class.

This is all one have to do in order to show downloaded images on appwidget listview's imageview.Further this tutorial is just a barebone implementation on how to download content(images or other) and populate them on appwidget listview. There may be better ways to do also and if anyone has done so in efficient manner please share us also.
Finally all the source code can be downloaded from my github. Till next time,enjoy life.

AQuery is an awesome library to work out with remote data be it simple json files,xml files or images,bitmaps to name a few. Go to AQuery to know more about this library. Now let us create a appwidget configuration activity. This appwidget configuration Activty is the Activity which gets launched when appwidget is placed on homescreen i.e. when appwidget is selected from widgets of phone and drag and dropped to homescreen. For any appwidget configuration activity to work it must be declared on

Now let us fill up our ConfigActivity. This Activity is launched once AppWidget is placed and it contains simply a Button. Upon click of ConfigActivity's Button, AppWidget is launched along with a Service is started to fetch our json data.

Read out the comments on ConfigActivity to know about its working. Now let’s create our Service whose main task is to

Fetch data from web

Populate AppWidget ListView with fetched data

An AppWidgetProvider is basically a BroadCastReciever,so whenever one needs to communicate to AppWidgetProvider,one must send broadcast from activity,service with necessary action string. And this is the pattern on which AppWidgetProvider works.Though this is not compulsory,try to create RemoteViews on AppWidgetProvider and call update from within AppWidgetProvider.
Let’s go through this one by one. First let’s create our remote data fetch service and name it as

Now let us modify our WidgetProvider.java to reflect the changes. Previously ,all the updates of widget were handled on onUpdate() method.But from now on onUpdate() will just start RemoteFetchService i.e. on every 30 min interval,to fetch new data RemoteFetchService will be called to fetch data from web and notify WidgetProvider with broadcast. Just like any BroadCastReceiver, WidgetProvider onReceive() method will receive the sent broadcast. And we just find out the broadcast action and carry out necessary updates through AppWidgetManger. Check out our modified WidgetProvider.java to know more

public class WidgetProvider extends AppWidgetProvider {
// String to be sent on Broadcast as soon as Data is Fetched
// should be included on WidgetProvider manifest intent action
// to be recognized by this WidgetProvider to receive broadcast
public static final String DATA_FETCHED = "com.wordpress.laaptu.DATA_FETCHED";
/**
* this method is called every 30 mins as specified on widgetinfo.xml this
* method is also called on every phone reboot from this method nothing is
* updated right now but instead RetmoteFetchService class is called this
* service will fetch data,and send broadcast to WidgetProvider this
* broadcast will be received by WidgetProvider onReceive which in turn
* updates the widget
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
Intent serviceIntent = new Intent(context, RemoteFetchService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i]);
context.startService(serviceIntent);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
// which layout to show on widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// RemoteViews Service needed to provide adapter for ListView
Intent svcIntent = new Intent(context, WidgetService.class);
// passing app widget id to that RemoteViews Service
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// setting a unique Uri to the intent
// don't know its purpose to me right now
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
// setting adapter to listview of the widget
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
svcIntent);
// setting an empty view in case of no data
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
return remoteViews;
}
/**
* It receives the broadcast as per the action set on intent filters on
* Manifest.xml once data is fetched from RemotePostService,it sends
* broadcast and WidgetProvider notifies to change the data the data change
* right now happens on ListProvider as it takes RemoteFetchService
* listItemList as data
*/
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(DATA_FETCHED)) {
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}

Finally the updated data is now referenced from ListProvider.java class,which just clones the ArrayList of RemoteFetchService class as

In this manner the fetched data from the web is now received ListProvider which in turn is reflected on our AppWidget. This approach of using public static ArrayList has a major flaw. When they are more than 2 AppWidgets and they at the same time fetch and begin to modify public static ArrayList of RemoteFetchService,then the last modified value will be taken by both the AppWidget which is incorrect for one of the widget. So,in order to resolve this,better use Database to store values with given AppWidget Id. And on RemoteViewsService or on RemoteViewsFactory,simply fetch the data from Database with that AppWidget Id.
I have put the entire project My Github. Till next time, enjoy life.

Finally I got a taste of App Widget while doing a recent project.So, I am going to publish a series of tutorial on App Widget which may include

app widget with listview

populate app widget listview with data from web

download images and show on imageview of appwidget with listview

setting update interval on appwidget with listview

how to make appwidget update work after phone reboot

Let’s first dive into basics of App Widget. 1: AppWidgetProvider This is governing body of App Widget. Meaning everything of App Widget is controlled from here. By control means

Widget Update

Widget Delete

Widget enabled/disabled

to name a few. 2: Xml file for AppWidgetProvider: Let’s make it an analogy like Activity has Layout.xml file,App Widget has appwidget-provider xml file @res/xml So every AppWidget must have these two building blocks. Let’s look upon Xml file for AppWidgetProvider and name it widgetinfo.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Activity to be launched,when you first install widget(drag n drop widget to homescreen) we will ignore this now -->
android:configure="com.wordpress.laaptu.ConfigActivity"
<!-- the layout of the widget,just like setContentView(id) of Activity-->
android:initialLayout="@layout/widget_layout"
<!-- Minimum width and height of the widget -->
android:minHeight="110dip"
android:minWidth="250dip"
<!-- Image to be seen when you go to select a widget -->
android:previewImage="@drawable/widget_preview"
<!-- How to resize the widget -->
android:resizeMode="vertical|horizontal"
<!-- Update interval i.e. @ this interval WidgetProvider onUpdate method will be called -->
<!-- Default is 30 min,you can set lower value,but it will take 30 min ,but it takes higher value than 30 min-->
<!-- 30 min =30x60x1000 -->
android:updatePeriodMillis="1800000"/>

For widget to be recognized,you must set it on AndroidManifest.xml just like you set Activity,in following manner

1:RemoteViewsService: Just consider this class as the class which tells the ListView of appwidget to take what type of data. By data meaning what RemoteViewsFactory.To make it more simple,if you have done ListView population,this class defines the Adapter for the ListView.Let us name RemoteViewsService as WidgetService.java

I have put comments on the code to let you understand different aspect of the AppWidgetProvider. Lastly,to make the AppWidget ListView to use Adapter,on must define RemoteViewsService on AndroidManifest.xml as well.

This is all,to show an AppWidget with ListView on it. You can checkout My Github to download all the codes of this tutorial. On next tutorial,I will show how to populate app widget listview from remote data(i.e from web). Till then Happy Coding

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

600 people reached the top of Mt. Everest in 2012. This blog got about 6,500 views in 2012. If every person who reached the top of Mt. Everest viewed this blog, it would have taken 11 years to get that many views.

In our ongoing project we are heavily relying on network calls i.e. fetching data from the server and posting data to the server.Previously,we used AsyncTask and right now we found an awesome open source library named Aquery. This library is equipped with large set of functionality like image loading from server,using HTTP POST and GET,easy XML parsing with Aquery implemented XmlDom, easy object mapping to server response and many others. Moreover, this library is very easy to use and implement and kudos to all developer of Aquery for making such an useful and powerful class.
While using Aquery we needed to perform cancel to any server request via user button click action.But, first lets start with small Aquery demo

My previous implementation on cancelling Aquery was outdated. So it wasn’t working anymore. Then a fellow developer named ariellopezf
had found the correct way to cancel Aquery of recent version. Though,I haven’t tested myself,I am completely relying on ariellopezf words and giving his solution of properly cancelling Aquery. See the comment’s of this post to know more about ariellopezf

This is all needed to cancel Aquery ajax request. And Aquery is very useful library to be used ,if app needs to communicate to server frequently and I urge every Android Developer to use it. I believe that someday Aquery will pave its usage to Android Framework
Till then enjoy coding,enjoy blogging 😉

Recently we uploaded a new project named Facebook Scheduler which allows the user to schedule wall post messages to his/her own timeline or to his/her friends’ and groups timeline.Here we relied heavily on android compatibility library,Fragment,FragmentAdapter,FragementActivity.

We used android compatibility library ,looking upon the examples provided and we didn’t go deep into it. And all the problem started right from there. After lots of null pointer exception and force closes ,we finally figured out how to communicate between Activity(Fragment Activity) and Fragment. So before diving into solution ,I would like you to explain in brief simple life cycle of Fragment

A Fragment is always dependent on FragmentActivity and it always rests or to say lives on FragmentActivity . So life cycle of Fragment depends upon the life cycle of FragmentActivity . Let’s now explain those portion of fragments life cycle that is important in this context

onAttach():
This signifies that the Activity has attached fragment on it and it doesn’t mean that all the views of Activity are created not it means that Activity is fully functional. This is just a point where you can reference the activity

onCreate():
Just a point which shows that Fragment is in the process of creation and in this method just try to access those values that you have saved on onSavedInstanceState(Bundle outState)

onCreateView():
Here we inflate the layout or simply create the view and further if you have to do anything that takes reference to Activity don’t do it like creating dialogs,accessing views of Activity etc because,this place doesn’t ensure that hosting Activity is fully functional

onActivityCreated():
This place signifies that our hosting Activity views are created and hosting Activity is functional and this is the right place to do all your Activity related task.

onResume():
This is the place where I prefer to do all the actions of the fragment

Now moving away from theory lets create a means of communication between Fragment and Activity. Communication should be done with interfaces and interface initialization should be done on Fragment.Lets consider a scenario
1: Fragment has a button named activityButton and Activity has button named fragmentButton on their respective layout

2: On click of activityButton Fragment sends data to Activity and on click of fragmentButton Activity sends data to fragment

3: Further Fragment has ListView,ProgressDialog,Adapters,just to show where to initialize those on fragment

Lets’ create two interfaces named FragmentCommunicator which is used to pass data from Activity to Fragment and ActivityCommunicator which is used to pass data from Fragment to Activity

Lets define our FragmentStatePagerAdapter named CustomFragmentAdapter and according to Android documentation better to use FragmentStatePagerAdapter than FragmentPagerAdapter to save the state of the fragments

This is just a mechanism on how to communicate between fragment and activity and it has one flaw.Since we initialized the fragmentCommunicator on CustomFragment and if we need to pass some data on onCreate() method of Activity like

fragmentCommunicator.passDataToFragment("Hello I am created");

Then it won’t be always certain that Fragment is created and its possible fragmentCommunicator may be null and result in app force close .If cases are like those,don’t pass values directly to Fragment but instead create a mechanism on Fragment to retrieve values of Activity.Let’s say our Fragment needs to look some value of Activity and then only do its work then

This is just a rough implementation,but you can devise a better way to implement this solution.Hope, you didn’t sleep halfway reading this.The reason ,I made this tutorial,is I couldn’t find a better explanation on the any site,on how to properly communicate between Activity and Fragment without lots of null pointer exception. And if you find any other better implementation than me ,do comment or forward me links. Till then, keep on coding 😉