2

I'm creating a simple android widget that will show small images.

I need to be able to set the width of the widget according to the num of images selected.

My widget:

    <?xml version="1.0" encoding="utf-8"?>
     <appwidget-provider
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:initialLayout="@layout/widget_layout"
        android:minHeight="72dp"
        android:minWidth="72dp"
        android:updatePeriodMillis="0" >
    </appwidget-provider>

And the Java class:

      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {


      SharedPreferences preferences =   PreferenceManager.getDefaultSharedPreferences(context);
      if(!url.equalsIgnoreCase(""))
      {
          url = preferences.getString("url", "");
      }

       // Get all ids
       ComponentName thisWidget = new      ComponentName(context,MyWidgetProvider.class);

    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {

      RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);


      // Register an onClickListener
      Intent intent = new Intent(context, MyWidgetProvider.class);

      intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

      PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteViews.setOnClickPendingIntent(R.id.open, pendingIntent);
      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }


     }

How can I change the minWidth and minHeight programmatically?

1 Answer 1

2

I guess it's not possible to change minWidth and minHeight programmatically; but you may try to use resizeMode to extend the widget layout. I hope this solution helps you.

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="320dp"
    android:minHeight="320dp"
    android:minResizeHeight="160dp"
    android:minResizeWidth="240dp"
    android:updatePeriodMillis="100"
    android:initialLayout="@layout/widget"
    android:autoAdvanceViewId="@+id/list_view"
    android:previewImage="@drawable/preview_image"
    android:resizeMode="vertical|horizontal"
    />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.