Search code examples
javaandroidwidgetimagebuttontoast

show a simple toast after click on imagebutton on android widget


I need help in adding action to a homescreen widget I want to add to my app. I've searched for a solution on many thread, but none of them worked for me.

All I want to do is toast a simple message after a click on an image button on my widget. If this works, I want to run a service intent after a click on this button.

I would be wonderful if you could add the code that you wrote in java.

This is the code of the xml widget

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#09C"
    android:padding="@dimen/widget_margin">

    <ImageButton
        android:id="@+id/widget_btn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/on" />
</RelativeLayout>

This is the class itself:

public class wow_shortcut extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        CharSequence widgetText = wow_shortcutConfigureActivity.loadTitlePref(context, appWidgetId);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wow_shortcut);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @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 onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        for (int appWidgetId : appWidgetIds) {
            wow_shortcutConfigureActivity.deleteTitlePref(context, 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
    }
}


Thank you and have a good day!


Solution

  • Starting a service from Android

    1. Create a class that extends BroadcastReceiver and add it to the AndroidManifest file.
    2. Create a. PendingIntent to start a Broadcast. And attach it to the widget view.

      val intent = Intent(context, MyBroadcast::class.java)
      
      val pendingIntent = PendingIntent.getBroadcast(
          context,
          MY_REQUEST_CODE,
          intent,
          PendingIntent.FLAG_UPDATE_CURRENT
      )
      
      setOnClickPendingIntent(R.id.widget_btn, pendingIntent)
      
    3. In your BroadcastReceiver, start the service.

      class MyBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            // Create new Intent to start your service.
        }
      }
      
    4. The Service may be stopped due to background restrictions and you may need to bring it in the foreground.

    Note: the code is in Kotlin, but it should be similar in Java.