title: Onclick Event for Android Widget
body: <p>I've read about 10 posts now about onclick events and Widgets and I understand they are implemented differently. However, despite the success other people are having I seem to be finding it impossible to get it to work. I'm using Android Studio 2.2 and have created a simple test app. I have a button called button. I want a toast to display when I press the button. </p>
  
  <p>I've copied the code as suggested in the other posts. Can someone take a look and see if I am doing anything wrong?</p>
  
  <pre><code>public class NewAppWidget extends AppWidgetProvider {
  
      private static final String MyOnClick = "myOnClickTag";
  
      protected PendingIntent getPendingSelfIntent(Context context, String action) {
          Intent intent = new Intent(context, getClass());
          intent.setAction(action);
          return PendingIntent.getBroadcast(context, 0, intent, 0);
      }
  
       void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                              int appWidgetId) {
  
          CharSequence widgetText = context.getString(R.string.appwidget_text);
          // Construct the RemoteViews object
          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
  
  
          views.setOnClickPendingIntent(R.id.button, getPendingSelfIntent(context, MyOnClick));
  
          views.setTextViewText(R.id.appwidget_text, widgetText);
  
  
  
          // Instruct the widget manager to update the widget
          appWidgetManager.updateAppWidget(appWidgetId, views);
      }
  
      //RemoteViews.setOnClickPendingIntent(R.id.button, getPendingSelfIntent(context, MyOnClick));
  
      @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
      }
      @Override
      public void onReceive(Context context, Intent intent) {
          super.onReceive(context, intent);//add this line
          if (MyOnClick.equals(intent.getAction())){
              //your onClick action is here
              //display in short period of time
              Toast.makeText(context, "msg msgasdasd", Toast.LENGTH_SHORT).show();
  
          }
      };
  }
  </code></pre>