Pedro Igor
Pedro Igor

Reputation: 19

How to open application by clicking widget

My WidgetProvider is thus:

package br.com.pedro.widget;

import android.appwidget.AppWidgetProvider;

public class MyWidgetProvider extends AppWidgetProvider {

}

How to show a widget that displays a layout and when clicked goes to my main class?

Upvotes: 1

Views: 2288

Answers (1)

Sureshkumar S
Sureshkumar S

Reputation: 313

Here replace MainActivity to your activity you want to open.

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        Toast.makeText(context, "Touched view ", Toast.LENGTH_SHORT).show();
        Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds).size());
        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app
            // widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

Upvotes: 3

Related Questions