How to update widget view when button is clicked in android?

I am developing an Android widget, and I have displayed some data of this widget from a web service and its performance. This is the code for this.

CODE

public class WatchWidget extends AppWidgetProvider{ private static final String TAG = "WatchWidget"; @Override public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ){ Log.i(TAG, "* onUpdate"); EBWeaterData ebwd=new EBWeaterData(); EBWeatreUtils.getWeatherFeeds(context, ebwd, "my url to get data"); RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.main); RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.test); //get data String day=ebwd.getDay_1()+ebwd.getDay_suffix_1()+" "+ebwd.getDay_name_1(); String minmaxtemp="Max Temp "+ebwd.getDay_max_temp_1()+"°C "+"Min Temp "+ebwd.getDay_min_temp_1()+"°C"; //set dat to views newView.setTextViewText(R.id.textView_day_name, day); newView.setTextViewText(R.id.textView_minmaxtemp, minmaxtemp); newView.setImageViewResource(R.id.ImageView_icon, R.drawable.sunny); views.addView(R.id.view_container, newView); ComponentName thisWidget = new ComponentName(context,WatchWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(context); manager.updateAppWidget(thisWidget, views); } } 

I have a button in this widget, now I want to update the widget view with different values ​​when the user clicks the button. I know what I should do with this pendingIntent, as shown below.

  Intent intent = new Intent(context, activitytogetNewdata.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.imageView1, pendingIntent); 

But he will start another activity, I do not want to go to another game. I just need to update the same kind of widgets with the new values ​​after clicking the button. Any idea

+4
source share
2 answers

I found a problem with my code. When I am going to update my view using pendingintent, it will dynamically add a view at every click of the button, because I used 2 layouts, so I solved the problem as follows (I use one layout instead of two)

CODE

 public class WatchWidget extends AppWidgetProvider{ private static final String TAG = "WatchWidget"; @Override public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ){ Log.i(TAG, "* onUpdate"); EBWeaterData ebwd=new EBWeaterData(); EBWeatreUtils.getWeatherFeeds(context, ebwd, "my url to get data"); RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.test); //get data String day=ebwd.getDay_1()+ebwd.getDay_suffix_1()+" "+ebwd.getDay_name_1(); String minmaxtemp="Max Temp "+ebwd.getDay_max_temp_1()+"°C "+"Min Temp "+ebwd.getDay_min_temp_1()+"°C"; //set dat to views newView.setTextViewText(R.id.textView_day_name, day); newView.setTextViewText(R.id.textView_minmaxtemp, minmaxtemp); newView.setImageViewResource(R.id.ImageView_icon, R.drawable.sunny); ComponentName thisWidget = new ComponentName(context,WatchWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(context); manager.updateAppWidget(thisWidget, newView); } 
+2
source

All Articles