Android App Widget - button for changing the contents of TextView

I am a little confused about Android app widgets. I understand that I can create a button to trigger an action, but I don’t see how to create a button to edit TextView text inside a widget. Can someone please tell me what I am missing?

Thank you very much in advance.

EDIT: I do not want the user to enter new text, I mean that the application automatically changes the text without any user interfaces (i.e. dialog boxes or actions).

+1
source share
2 answers

EDIT: OK, so you install getBroadcast PendingIntent on your button with setOnClickPendingIntent and update (or call the update method) with the new recipient text. Your AppWidgetProvider class may be your receiver (you can catch your intention in onReceive ), otherwise you will create a new class that extends BroadcastReceiver .

EDIT2 (sample code):

I did not run / compile this code, so I hope it will not have errors. This is a simple helper method for getting a basic update, pending intent, using a special action:

public PendingIntent getRefreshPendingIntent(Context context, int appWidgetId){ Intent intent = new Intent("my.package.ACTION_UPDATE_WIDGET"); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } 

Hitting a custom action on onReceive in an overridden class of AppWidgetProvider

 @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); //handle other appwidget actions if (intent.getAction().equals("my.package.ACTION_UPDATE_WIDGET")) { //some code here that will update your widget } //handle other appwidget actions } 

You also need to add an IntentFilter for the custom action to the manifest in the application recipient declaration.

 <receiver android:name="my.package.MyWidgetProviderClass" android:label="MyWidget"> <intent-filter> <action android:name="my.package.ACTION_UPDATE_WIDGET"/> <!-- other intent filters --> </intent-filter> <!-- meta data pointing to widget xml file --> </receiver> 

Finally, you apply the pending intent for your RemoteViews whenever you build it, so when the button is pressed, it will send the action my.package.ACTION_UPDATE_WIDGET, which will be captured by your AppWidgetProvider, where you can (or call the method) update AppWidget

 myRemoteViews.setOnClickPendingIntent(R.id.id_of_update_button, getRefreshPendingIntent(context, appWidgetId); 
+3
source

This code updates the widget, when you click on it, you can edit it and change the listener to your button:

 public class MyWidgetProvider extends AppWidgetProvider { private static final String ACTION_CLICK = "ACTION_CLICK"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Get all ids ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class); int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); for (int widgetId : allWidgetIds) { // Create some random data int number = (new Random().nextInt(100)); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); Log.w("WidgetExample", String.valueOf(number)); // Set the text remoteViews.setTextViewText(R.id.update, String.valueOf(number)); // Register an onClickListener Intent intent = new Intent(context, MyWidgetProvider.class); intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent); appWidgetManager.updateAppWidget(widgetId, remoteViews); } } } 

full tutorial here .

+2
source

All Articles