Search code examples
androidwidgetandroid-widgetprogressandroid-wheel

Showing a progress wheel in a widget


I have a widget that gets updated when I press it, but I would like to show a progress wheel while the widget gets updated.

//remove text      
        updateViews.setViewVisibility(R.id.widget_textview, View.GONE);
        updateViews.setViewVisibility(R.id.widget_dkk, View.GONE);

// show progress wheel

updateViews.setViewVisibility(R.id.widget_Pross, View.VISIBLE);



// task that take time are locatet here a GetHTTP function



//Show text again       
        updateViews.setViewVisibility(R.id.widget_textview, View.VISIBLE);
        updateViews.setViewVisibility(R.id.widget_dkk, View.VISIBLE);

// Remove progress wheel

   updateViews.setViewVisibility(R.id.widget_Pross, View.GONE);

ALL this are locatet this in my widget update class:

public static RemoteViews buildUpdate(Context context) {

But the progress wheel does not show. It's like the widget only get updated one time, and thereby not showing the progress wheel while computing in the GetHttp.

EDIT:

// Remove rext and show progress wheel
    updateViews.setViewVisibility(R.id.widget_textview, View.GONE);
        updateViews.setViewVisibility(R.id.widget_dkk, View.GONE);
        updateViews.setViewVisibility(R.id.widget_Pross, View.VISIBLE);

    ComponentName thisWidget = new ComponentName(context, SaldoWidget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(thisWidget, updateViews);


//    GetHttp code here / long computing task her

 //Show text and remove progress wheel 
 updateViews.setViewVisibility(R.id.widget_textview, View.VISIBLE);
            updateViews.setViewVisibility(R.id.widget_dkk, View.VISIBLE);
            updateViews.setViewVisibility(R.id.widget_Pross, View.GONE);
            manager.updateAppWidget(thisWidget, updateViews);

Solution

  • You have to call AppWidgetManager.updateAppWidget() every time you want to change the layout.

    That means you have to hide the original widget views and show the progressbar in your RemoteViews. After that, call updateAppWidget() to change the layout and display the progressbar. Then execute your downloadtask. After that, populate the views with the fetched data, hide the progress indicator and call updateAppWidget() again to change it back to the normal layout.

    The name RemoteViews may make this a bit confusing at the start. It's actually not a bunch of Views that you can control remotely from your app. It's a group of views that you build up to your needs and then you give it to the AppWidgetManager to publish it.