4

I’m developing an android widget. In the widget I have a text field and two buttons. When I press button1 I want the text field to show a certain text, like “one,” and if I click button2, the text field will show like “2.”

the onUpdate() looks like this :

 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ super.onUpdate(context, appWidgetManager, appWidgetIds); 

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);

    Intent intent = new Intent(context, CalculatorReceiver.class);
    intent.setAction(ACTION_WIDGET_RECEIVER);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

    PendingIntent actionPendingIntent =  PendingIntent.getBroadcast(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.one, actionPendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); }

and the onReceive() method looks like this:

@Override public void onReceive(Context context, Intent intent) {

    super.onReceive(context, intent);

    if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)){

         Toast.makeText(context, "Hi I'm 1", Toast.LENGTH_LONG).show();

         }}

Now, in the widget, I’m only able to show a Toast when a button pressed, but I want to get the textView and change its value. How do I get and change the textView?

fullmoon
  • 8,030
  • 5
  • 43
  • 58
  • this question may be useful [enter link description here][1] [1]: http://stackoverflow.com/questions/6944553/android-appwidget-textview-not-updating –  Jan 14 '14 at 09:04
  • this [question][1] may be useful!!! [1]: http://stackoverflow.com/questions/6944553/android-appwidget-textview-not-updating –  Jan 14 '14 at 09:09

2 Answers2

6
  • You can't get the values of views from your widget.

  • You can update TextView of the widget by using RemoteViews remoteViews.setTextViewText(R.id.text_view_id, "Your text");

sromku
  • 4,663
  • 1
  • 36
  • 37
  • Hi, yea I know, but how do I update the TextView if the button pressed? I’m only able to recognize button press using: remoteViews.setOnClickPendingIntent(R.id.one, actionPendingIntent); and the actionPendingIntent will implement onReceive() method. i can’t get the TextView in the onRecieve() method because it will give me NullPointerException in the Receiver calass. – fullmoon Jun 05 '13 at 21:19
  • You can create again the `RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main)` in `onReceive` method and then update the text view. Or i missed your question.. – sromku Jun 05 '13 at 21:41
  • After this, also do `appWidgetManager.updateAppWidget(appWidgetId, remoteViews);`. –  Apr 16 '15 at 11:57
1

Please try to add the below lines in onReceive()

remoteViews.setTextViewText(R.id.widget_appNameView, "text view updated");

ComponentName componentName = new ComponentName(context, WidgetProvider.class); AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);

jettimadhuChowdary
  • 1,058
  • 1
  • 13
  • 23