Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing TextView text in an Android Widget

Tags:

android

widget

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?

like image 855
fullmoon Avatar asked Mar 24 '23 16:03

fullmoon


1 Answers

  • 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");

like image 160
sromku Avatar answered Apr 01 '23 02:04

sromku