Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button click event for android widget

I have an android widget that fetches data from a server every 10 minutes and display's it on the screen.
I'd like to add a "Refresh" button to that widget.
When the user clicks that button I'd like to run the method that fetches the information from the server.
Adding an event handler to a button in an application is very easy, however I couldn't find an example for a widget.
I'd like to get some help with adding a function to a button click in a widget.

like image 552
Sharon Haim Pour Avatar asked Feb 10 '13 13:02

Sharon Haim Pour


People also ask

How to set click event on button in android?

When the user clicks a button, the Button object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

What is setOnClickListener android?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

How to give link to button in android studio?

Create a String (named hyperlink) in string. xml and give its value like link and then set property android:setText="@string/hyperlink" to Button.

How to create OnClickListener in android studio?

Creating Anonymous View.OnClickListenerLink the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter. Basically it's creating an anonymous subclass OnClickListener in the parameter.


1 Answers

Here is one example more that should help:

package com.automatic.widget;  import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews;  public class Widget extends AppWidgetProvider {      private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";      @Override     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {         RemoteViews remoteViews;         ComponentName watchWidget;          remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);         watchWidget = new ComponentName(context, Widget.class);          remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));         appWidgetManager.updateAppWidget(watchWidget, remoteViews);     }      @Override     public void onReceive(Context context, Intent intent) {         // TODO Auto-generated method stub         super.onReceive(context, intent);          if (SYNC_CLICKED.equals(intent.getAction())) {              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);              RemoteViews remoteViews;             ComponentName watchWidget;              remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);             watchWidget = new ComponentName(context, Widget.class);              remoteViews.setTextViewText(R.id.sync_button, "TESTING");              appWidgetManager.updateAppWidget(watchWidget, remoteViews);          }     }      protected PendingIntent getPendingSelfIntent(Context context, String action) {         Intent intent = new Intent(context, getClass());         intent.setAction(action);         return PendingIntent.getBroadcast(context, 0, intent, 0);     } } 
like image 193
Erti-Chris Eelmaa Avatar answered Sep 16 '22 15:09

Erti-Chris Eelmaa