0

I found the following function that allows you to insert a Widget.

Specifying: createWidget(View view, String packageName, String className)

The problem at the moment is that:

  • Some widgets have a configuration phase, such as a WidgetConfigureActivity. But it is not called when the widget is inserted into the layout, so some widgets are not inserted correctly.

  • Some widgets, take up little space. But at the time of insertion take all the space available within the layout.

Any suggestions?

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/homeScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/viewWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical" />

</LinearLayout>

code:

public boolean createWidget(View view, String packageName, String className) {
            // Get the list of installed widgets
            AppWidgetProviderInfo newAppWidgetProviderInfo = null;
            List<AppWidgetProviderInfo> appWidgetInfos;
            appWidgetInfos = mAppWidgetManager.getInstalledProviders();
            boolean widgetIsFound = false;
            for (int j = 0; j < appWidgetInfos.size(); j++) {
                if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className)) {
                    // Get the full info of the required widget
                    newAppWidgetProviderInfo = appWidgetInfos.get(j);
                    widgetIsFound = true;
                    break;
                }
            }

            if (!widgetIsFound) {
                return false;
            } else {
                Log.v("Class", "Ok*");
                // Create Widget
                int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
                AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
                hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);

                // Add it to your layout
                LinearLayout widgetLayout = findViewById(R.id.mylockscreen);
                widgetLayout.addView(hostView);

                // And bind widget IDs to make them actually work
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);

                    if (!allowed) {
                        // Request permission - https://stackoverflow.com/a/44351320/1816603
                        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
                        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
                        final int REQUEST_BIND_WIDGET = 1987;
                        startActivityForResult(intent, REQUEST_BIND_WIDGET);
                    }
                }

                return true;
            }
        }

ListWidget:

Add Widget Size:

Real Widget Size:

Paul
  • 3,644
  • 9
  • 47
  • 113
  • what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible – HeyAlex Nov 15 '18 at 11:35
  • @HeyAlex: Let me know if I have explained this well. – Paul Nov 15 '18 at 14:11
  • are you trying to make a launcher? – HeyAlex Nov 15 '18 at 15:45
  • I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well. – Paul Nov 15 '18 at 16:01

0 Answers0