Android: How to update widget text on creation - android

I have seen several questions regarding how to update widgets but nothing is helping me with my issue.
I created a widget that has a textview that I want to dynamically update upon widget creation.
I have a configuration activity that is called when adding the widget on the screen. I store the value in the sharedpreferences and retreive it onUpdate. The problem is that the widget doesn't get updated. Any idea how it can be done? I do not want to update the textview after a click on the widget, just get the correct text to be displayed on creation.
public class AndroidWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context,
AndroidWidget.class);
int[] allWidgetInstancesIds = appWidgetManager
.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetInstancesIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// Create an intent that when received will launch the PopUpActivity
Intent intent = new Intent(context, AndroidWidget.class);
intent.setAction(SHOW_POPUP_DIALOG_ACTION);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
widgetId, intent, 0);
// Set up the onClickListener of the widget
remoteViews.setOnClickPendingIntent(R.id.myText, pendingIntent);
SharedPreferences prefs = context.getSharedPreferences(
String.valueOf(widgetId), Context.MODE_PRIVATE);
remoteViews.setTextViewText(R.id.myText,
prefs.getString("storedtext", null));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
This actually updates the textview but only after clicking or creating another widget.

#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//set widget id
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intentService = new Intent(context, WidgetService.class);
intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent clickIntent = new Intent(context, MainActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setTextViewText(R.id.currency, " Dollar");
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
This code works for me. You may try this.

Related

How to refresh widget listview when button refresh is clicked?

I'm creating a Widget with ListView on it. Displaying the result from the Web Service in the ListView is okay. I created a class which is my WidgetService that extends to RemoteViewsService and on RemoteViewsFactory I call my ListViewProvider that has the Web Service. I added a refresh button that when clicked it will call the WidgetService again to created the list view.
Here is my code in my WidgetProvider
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
//ListView
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
//REFRESH
PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, svcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, updatePendingIntent);
return remoteViews;
}
As you can see in the refresh part I tried using PendingIntent to call the WidgetService but when testing it doesn't refresh the list.
Thank you for the help in advance.
In my onUpdateI set an Action to be called in my onReceive, and also put the appWidgetIds to be used as well in the onReceive
Intent refreshIntent = new Intent(context, WidgetProvider.class);
refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
then in onReceive I call the onUpdate
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds != null && appWidgetIds.length > 0) {
Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
}
}
}
arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());
and when click refreash button
arrayList.clear();
arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());

appWidgetIds only contain one id

I have appWidgetProvider that updates views of each widget located on homescreen and each widget has a button which triggers updating the view but apparently no matter which button I press on whichever widget only and always the one of them is being updated
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int widgetId: appWidgetIds){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
Intent update = new Intent(context, QuotesAppWidgetProvider.class);
Bundle bundle = new Bundle();
bundle.putIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetId});
update.putExtras(bundle);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, update, 0);
views.setOnClickPendingIntent(R.id.widget_refresh, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, views);
}
}
Let's say I have widget with id=30 and widget with id=31 and I click the refresh button on widget30 and then widget30 is being updated which is desired but when I click on refresh button on widget31 then widget30 is being updated for I don't know what reason.
It took me a bit but I finally figured it out. All of the widgets had pendingintents with the same request code and since it was broadcast it overwritten all of them with one. This was the line that helped me:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widgetId, update, PendingIntent.FLAG_UPDATE_CURRENT);
Use this code and check.
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int widgetId: appWidgetIds){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
Intent update = new Intent(context, getClass());
update.setAction("actUpdateWidgets");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, update, 0);
views.setOnClickPendingIntent(R.id.widget_refresh, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if (intent.getAction().equalsIgnoreCase("actUpdateWidgets"))
{
Intent i = new Intent(context, QuotesAppWidgetProvider.class);
i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int ids[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, QuotesAppWidgetProvider.class));
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
context.sendBroadcast(i);
}
}

How to launch an activity after clicking a widget?

I want to create a simple widget with an icon to launch my app.
I have the widget and its layout working properly, but I can't find the way to make it launcher the app when clicking it.
This is the widget class:
public class LauncherWidget extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager) {
Intent intent = new Intent(context, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Attach the intent to the widget's button.
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
}
}
I have never created a widget so I have no idea what to do here.
I have checked round here for more info but all I can find are widgets that update its content when clicking, but I tried to use their code but it isn't working.
Thanks in advance.
This is what worked for me...
The onUpdate method code should be:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
try {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setComponent(new ComponentName(context.getPackageName(),
"Activity.class"));
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
} catch (ActivityNotFoundException e) {
Toast.makeText(context.getApplicationContext(),
"There was a problem loading the application: ",
Toast.LENGTH_SHORT).show();
}
}
}
Use this snippet in onUpdate() method of your widget AppWidgetProvider class:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
Intent configIntent = new Intent(context, Activity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

I want to start a configure activity on click of android widgets?

For below code...
below code is for the android widget
the same code is on this link Clickhere
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds)
{
updateAppWidget(context, appWidgetManager, widgetId);
}
}
protected static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
//Inflate layout.
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.time_weight_layout);
//Update UI.
remoteViews.setTextViewText(R.id.tvTime, Utility.settemp());
remoteViews.setTextViewText(R.id.tvTitle, Utility.gettitle()); //When user click on the label, update ALL the instances of the widget.
Intent labelIntent = get_ACTION_APPWIDGET_UPDATE_Intent(context);
labelIntent.setData(Uri.withAppendedPath(Uri.parse("abc" + "://widget/id/"), String.valueOf(appWidgetId)));
PendingIntent labelPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, labelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tvTime, labelPendingIntent);
Log.d("updateAppWidget", "Updated ID: " + appWidgetId);
//Call the Manager to ensure the changes take effect.
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
protected static Intent get_ACTION_APPWIDGET_UPDATE_Intent(Context context)
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), TimeWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
return intent;
}
here Main activity is configure activity I want to start MainActivity on click of widget
Here's a youtube link for the same.
It's only 6 min tut.

Can't update AppWidget RemoteViews after re-enter the activity

I am trying to manually update my ListView in the appwidget when I make some data changed in the activity, and I can do it successfully when I first time install the app on the phone.
But after I leave the app and re-enter it again, I can't update my remoteviews.
I call the AppWidgetUpdater to help me update the remoteviews:
AppWidgetUpdater appWidgetUpdater = new AppWidgetUpdater(context);
appWidgetUpdater.updateAppWidget();
AppWidgetUpdater:
public void updateAppWidget(){
ComponentName appWidget = new ComponentName(context, SimpleTODOAppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(appWidget);
Intent intent = new Intent(context, SimpleTODOAppWidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
context.sendBroadcast(intent);
}
AppWidgetProvider:
public class SimpleTODOAppWidgetProvider extends AppWidgetProvider {
private Context context;
private AppWidgetManager appWidgetManager;
private int[] appWidgetIds;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
Log.d("TEST", appWidgetIds.toString() + " from provider");
}
}
#SuppressWarnings("deprecation")
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_main);
Intent svcIntent = new Intent(context, ListViewAdaptorService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.appwidget_listview,
svcIntent);
remoteViews.setEmptyView(R.id.appwidget_listview, R.id.appwidget_addbutton);
return remoteViews;
}
}
Finally I find what the problem is.
I should use:
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.appwidget_listview);
instead of:
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
to update my ListView. I should have done some work to get familiar with ListViewAdaptor before I used Adapter in the AppWidget...

Categories

Resources