Update a widget when clicked, if user created multiple widgets - android

My app is an Android widget. It adds a button on home Screen; if a user clicked in this Button it will open a window. This window is waiting an user input. It has to write a value and this value will change the text in the Button.
The problem is if the user creates many widgets and clicked on the Button in any widget and write a value the value will be set only on the last created widget.
How can I make the Button change text on his widget?
And this is my code
public class Widget_note extends AppWidgetProvider {
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
the_context = context ;
the_appWidgetManager = appWidgetManager ;
the_appWidgetIds = appWidgetIds ;
Intent open_activity087 = new Intent();
open_activity087.setClassName("syr.hamza.app.mynotes_v2", "syr.hamza.app.mynotes_v2.SetWidgetValue");
open_activity087.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(open_activity087);
/*this will be open activity Immediately after creating widget to enter a value
* and will calling a set_value_in_widget() Function (at Below) with the value
* and this don't have any problem*/
Intent intent2 = new Intent(context, EditWidgetValue.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds,views);
/* this will be open activity if clicked on the button and get the value from user
* and calling set_value_in_widget() Function
* here is a problem */
}
public static Context the_context ;
public static AppWidgetManager the_appWidgetManager ;
public static int[] the_appWidgetIds ;
public static void set_value_in_widget(String value) {
RemoteViews views2 = new RemoteViews(the_context.getPackageName(), R.layout.widget_layout);
views2.setTextViewText( R.id.widget_button , value ) ;
the_appWidgetManager.updateAppWidget(the_appWidgetIds, views2 );
}
}

Keep all ids after a widget instance creating into SharedPreferences and on the OnUpdate read it from preferences and use instead the_appWidgetIds. Also you will need to remove them from preferences on onDelete.

Related

Widget refresh from inside application

Good Day! I want to add text from inside application to widget, i have a main activity and it has list view and lots of text contents and it has a button to add the text to widget via shared preference, it's works fine when i close the widget and recreate it only, otherwise it's not automatically refresh.if anyone know; how to solve this please help me. here i attached the widget code below.
public class WidgetMaster extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intentHome = new Intent(context, MainActivity.class);
PendingIntent pendingIntentHome = PendingIntent.getActivity(context, 0, intentHome, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_master);
views.setOnClickPendingIntent(R.id.wid_home, pendingIntentHome);
final SharedPreferences sharedPreferencestoWi = context.getSharedPreferences(String.valueOf(R.string.addTextToWidgetPref), MODE_PRIVATE);
String forWidget = sharedPreferencestoWi.getString("textToWidget", "");
String dum = "add from reading";
if(forWidget.equals("")){
views.setTextViewText(R.id.dum_appwidget_text, dum);
appWidgetManager.updateAppWidget(appWidgetId, views);
} else {
views.setTextViewText(R.id.appwidget_text, forWidget);
views.setViewVisibility(R.id.appwidget_text, 0);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
How do you update the widget from mainActivity?
In the widget configuration file you can specify a fixed update interval. The smallest update interval is 1800000 milliseconds (30 minutes).
But its better to update widget programmatically, to do this You should send a broadCast to update widget and use a
method like alarmManager or Handler for a repeating task for example you can use the following broadCast to update widget form mainActivity:
Intent intent = new Intent(this, WidgetMaster.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(getApplication())
.getAppWidgetIds(new ComponentName(getApplication(),WidgetMaster.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

launch update of listview inside appwidget when button is pressed android

I have an appwidget which contains a listview and a buton
the listview gives data from remote mysql database mysql and it display it
but the button I want to us it to refresh listview from appwidget directly and don't wait for time defined in appwidgetinfo
here is my widgetprovider class :
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);
}
}
}
I used the code-source of this tutorial here
I added the button in the layout with refresh_button id
but I don't know how to launch update of listview when user press it
how can I fix this issue
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
add the above code in AppWidgetProvider.onUpdate method this will give call to your onDataSetChanged() method with the help of that you can refresh the listview
I had the same problem but finally solved it

Updating text in a widget on an event

I created a class that downloads some text from the internet and I want to take that text and update a TextView in my widget. I know that the event (OnDownloadCompleteListener) is getting triggered because I'm Logging it but I can't figure out how to update the TextView from within that event. I know it's a newbie mistake, just not sure what I'm missing.
public class Widget extends AppWidgetProvider{
InternetText internettext; //Handles downloading the text from the internet
RemoteViews views;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.tvWidgetVerse, pendingIntent);
internettext = new InternetText(context);
internettext.setOnDownloadCompleteListener(new OnDownloadCompleteListener() {
#Override
public void onEvent() {
TheText thetext = internettext.downloadedText(); //The text object
Log.i("", "Widget Text Downloaded " + thetext.getText()); //This fires so I know we've downloaded the text
TextStyling textStyle = new TextStyling();
//*****THIS IS WHERE I'M HAVING THE PROBLEM********
views.setTextViewText(R.id.tvWidgetText, Html.fromHtml(textStyle.boldWords(thetext.getText()))); //this never updates
}
});
internettext.getText();
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Ok, I figured it out. I have to call appWidgetManager.updateAppWidget() from within the event. I knew it was a no-brainer...
appWidgetManager.updateAppWidget(appWidgetId, views);
which also means I have to make appWidgetManager and appWidgetId final
public void onUpdate(Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
...
}

How to update view of a widget on button click in android?

i am developing android widget and i displayed some data on that widget from web service and its working fine.Hear is the code for that.
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 on that widget now i want to update the widget view with different values when user clicks on the button.I know I have to do with it pendingIntent like following.
Intent intent = new Intent(context, activitytogetNewdata.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
But it will start another activity i dont want to go to anther activity.I just need to do is to update the same widget view with new values after button click.Any Idea??
I have found the problem of my code.when i going to update my view using pendingintent it will dynamically add view on each and every button click because i have used 2 layout hear so i solved the problem as follows.(i use one layout instead of using 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);
}
Go through this tutorial.
Visit http://www.developer.com/ws/android/programming/Handling-User-Interaction-with-Android-App-Widgets-3837531.htm
They have solved the problem what you have.

Android adding more widgets with different content

I am trying to create an application with a widget. When the user places the widget on the desktop a listview should come up with a list of items. The user selects an item then the widget is created with the respective text related to that item. For simplicity I am using now an alertdialog. The input text should appear only in the widget I create. It should not appear in the other widgets. How am I supposed to do that? I have not found anyhing about this.
The AppWidgetProvider:
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i("INFO", "AppWidgetProvider");
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
//Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
//intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
//context.startService(intent);
Intent intentA = new Intent(context.getApplicationContext(), NoteListToWidget.class);
intentA.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentA);
}
}
The service:
public class UpdateWidgetService extends Service {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
String value;
Dialog dialog_newitem;
#Override
public void onStart(Intent intent, int startId) {
Log.i("INFO", "UpdateWidgetService");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(), MyWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
int number = (new Random().nextInt(100));
String v = GlobalVars.getValue();
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
//remoteViews.setTextViewText(R.id.update, "Random: " + String.valueOf(number));
remoteViews.setTextViewText(R.id.update, v);
Intent configIntent = new Intent(this, Notepad.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_ONE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetlayout, configPendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
This is the Activity with the AlertDialog (the latter listview):
public class NoteListToWidget extends Activity {
Dialog dialog_newitem;
EditText et_newitem;
String value;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog_newitem = new Dialog(NoteListToWidget.this);
dialog_newitem.setContentView(R.layout.dialog_productlists_grp_capitalized);
dialog_newitem.setTitle("Select");
dialog_newitem.setCancelable(true);
et_newitem = (EditText) dialog_newitem.findViewById(R.id.et_item_name);
Button btn_Save = (Button) dialog_newitem.findViewById(R.id.btn_save_pr);
btn_Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
value = et_newitem.getText().toString();
GlobalVars.setValue(value);
dialog_newitem.dismiss();
/*
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.myapp.app.UpdateWidgetService");
startService(serviceIntent);
*/
Intent intent = new Intent(NoteListToWidget.this, com.myapp.app.UpdateWidgetService.class);
startService(intent);
}
});
Button btn_Cancel = (Button) dialog_newitem.findViewById(R.id.btn_cancel_pr);
btn_Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog_newitem.dismiss();
}
});
dialog_newitem.show();
}
}
I don't how and from where should I call the NoteListToWidget activity and how to make this whole thing work.
First of all it is impossible to answer your question in full. This is because it seems you are missing a lot of things about how app-widgets are working, so you must read more about them. However I will give you a place to start.
The onUpdate method of the AppWidgetProvider is called each time an app-widget that it is already placed on the desktop needs an update. That is, even if you manage to display your list from there and the user cancels the operation there is NO way to remove your app-widget from the onUpdate method.
Furthermore when the onUpdate method finishes the AppWidgetProvider is destroyed, so even if you manage to display your list from there there is NO way to get the items that user selected. Long story short, you canNOT do it from onUpdate method and you canNOT do it by this approach.
However, you CAN do it by using a configuration activity in a specified point of the app-widget's creation process but this cannot be answered in the context of your question because you must first understand how the app-widget system works in Android. The best place to start is this link. I had the same problems with my first app-widget application (and almost the same concerns with you) but then I started to read and read, finally after 1.5 months I 've managed to make something like the thing you want to do. So don't say
How am I supposed to do that? I have not found anyhing about this
and start reading, this is the life with Android. It's all in the above link, there are also very good open-source projects in google code like this weather widget which allows the user to configure it BEFORE it is placed on the desktop.
Hope this helps...

Categories

Resources