Hvck
Hvck

Reputation: 21

Android Homescreen Widget, call void function using a button

Im stuck 2 days on this thing, in my widget activity i have this void which i want to call when a button is pressed in the widget.

i want to call this:

 public void sendSMS() {

    smsManager.sendTextMessage("0123456789", null, SMSTEXT, null, null);

}

the xml layout of the button

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/send_button"
    android:layout_marginStart="54dp"
    android:layout_centerVertical="true"
    android:layout_alignStart="@+id/appwidget_text"

    />

Upvotes: 0

Views: 35

Answers (1)

Hvck
Hvck

Reputation: 21

Found it!

This needs to be in the onUpdate method

    Intent intent = new Intent(context, ParkingWidget.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    RemoteViews newviews = new RemoteViews(context.getPackageName(), R.layout.parking_widget);
    newviews.setOnClickPendingIntent(R.id.send_button, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds[0], newviews);

and you need to overide onRecive like this

@Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    sendSMS();  // code goes here

}

Upvotes: 1

Related Questions