[FIXED] Update textview inside Widget from App activity in Xamarin Android?


I have a widget which contains on TextView. From MainaActivity I am trying to update widget data as more data is added from main activity I want widget to refresh

class TodoWidget : AppWidgetProvider

    private void SetTextViewText(RemoteViews widgetView)
       // widgetView.SetTextViewText(Resource.Id.widgetMedium, new TodoDBR().GetAllRecordsForWidget());
        widgetView.SetTextViewText(Resource.Id.widgetSmall, new TodoDBR().GetAllRecordsForWidget());

In Main activity, after adding data I am trying to call method to update widgets but it is not working

AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(this.Context);
        var manager = AppWidgetManager.GetInstance(this.Context);
        var componentName = new ComponentName(Context.PackageName, Java.Lang.Class.FromType(typeof(TodoWidget)).Name);

        int[] appWidgetIds = appWidgetManager.GetAppWidgetIds(componentName);
            appWidgetManager.NotifyAppWidgetViewDataChanged(appWidgetIds, Resource.Layout.TodoWidget);

It is not updating my widget


If have found answer my self just incase anyone else have same issue here is solution

RemoteViews remoteViews = new RemoteViews(Context.PackageName, Resource.Layout.TodoWidget);
        ComponentName thisWidget = new ComponentName(this.Context, Java.Lang.Class.FromType(typeof(TodoWidget)).Name);
        remoteViews.SetTextViewText(Resource.Id.widgetSmall, new TodoDBR().GetAllRecordsForWidget());
        AppWidgetManager.GetInstance(this.Context).UpdateAppWidget(thisWidget, remoteViews);

Answered By – WasimMalik88

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published