Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically add the widget to home screen in android

I have developed android widget app and its working fine. Now my client asks that, when the user installed this app, it automatically needs to place on homescreen top position. How to do this? please help me.

like image 896
Murugan Vadivel Avatar asked Apr 03 '14 06:04

Murugan Vadivel


People also ask

How do I add multiple widgets to my Android home screen?

Select Apps and widgets that you want to add to the home screen. Make sure you switch to Widgets on the screen that is opening up. Find the Multicon widgets and select the one you want to use by holding down your finger on it and moving it to the location you want it to be displayed.


2 Answers

  1. Create Widget Provider Class
  2. Put into Manifest
  3. On button click which you wanna add to home

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    AppWidgetManager mAppWidgetManager = getSystemService(AppWidgetManager.class);

    ComponentName myProvider = new ComponentName(AddWidgetActivity.this, AppWidgetSmall.class);

    Bundle b = new Bundle();
    b.putString("ggg", "ggg");
    if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
         Intent pinnedWidgetCallbackIntent = new Intent(AddWidgetActivity.this, AppWidgetSmall.class);
         PendingIntent successCallback = PendingIntent.getBroadcast(AddWidgetActivity.this, 0,
                 pinnedWidgetCallbackIntent, 0);

         mAppWidgetManager.requestPinAppWidget(myProvider, b, successCallback);
    }
}
like image 152
jay patoliya Avatar answered Sep 28 '22 14:09

jay patoliya


As of Android O, In your app, you can create a request for the system to pin a widget onto a supported launcher.

  1. Create the widget in your app's manifest file
  2. Call the requestPinAddWidget() method

See the bottom part of this page: https://developer.android.com/preview/features/pinning-shortcuts-widgets.html

like image 45
Johan Franzén Avatar answered Sep 28 '22 14:09

Johan Franzén