Skip to content Skip to sidebar Skip to footer

Dynamically Adjusting Widget's Content And Layout To The Size The User Defined Through Resize. Android

Android design pattern guide says widget's content and layout can be dynamically adjusted to the size the user defined through resize operation here: Design guide for widgets Examp

Solution 1:

Thanks to A--C , this is possible for Jellybean and above devices and is simple to implement. Below is the sample code using onAppWidgetOptionsChanged method

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)@OverridepublicvoidonAppWidgetOptionsChanged(Context context,
        AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {

    Log.d(DEBUG_TAG, "Changed dimensions");

    // See the dimensions andBundleoptions= appWidgetManager.getAppWidgetOptions(appWidgetId);

    // Get min width and height.intminWidth= options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
    intminHeight= options
            .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);

            // Obtain appropriate widget and update it.
    appWidgetManager.updateAppWidget(appWidgetId,
            getRemoteViews(context, minWidth, minHeight));

    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,
            newOptions);
}

/**
 * Determine appropriate view based on width provided.
 * 
 * @param minWidth
 * @param minHeight
 * @return
 */private RemoteViews getRemoteViews(Context context, int minWidth,
        int minHeight) {
    // First find out rows and columns based on width provided.introws= getCellsForSize(minHeight);
    intcolumns= getCellsForSize(minWidth);

    if (columns == 4) {
        // Get 4 column widget remote view and return
    } else {
                    // Get appropriate remote view.returnnewRemoteViews(context.getPackageName(),
                R.layout.quick_add_widget_3_1);
    }
}

/**
 * Returns number of cells needed for given size of the widget.
 * 
 * @param size Widget size in dp.
 * @return Size in number of cells.
 */privatestaticintgetCellsForSize(int size) {
  intn=2;
  while (70 * n - 30 < size) {
    ++n;
  }
  return n - 1;
 }

Solution 2:

@Choletski @azendh

After changing layouts some click events are not called anymore

I solved this problem by creating function that setOnClickPendingIntent on view, and then return it.

For example the code is like

private RemoteViews getConfiguredView(RemoteViews remoteViews, Context context){

    IntentrefreshIntent=newIntent(context, EarningsWidget.class);
    refreshIntent.setAction(REFRESH_ACTION);
    PendingIntenttoastPendingIntent= PendingIntent.getBroadcast(context, 3, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.refreshButton, toastPendingIntent);
    return remoteViews;
}

And than function is called where you do "Get appropriate remote view."

returngetConfiguredView(newRemoteViews(context.getPackageName(), R.layout.activity_widget), context);

Solution 3:

Based on Gogu response and according this answer about how to get widget size, I'm created this WidgetClass in Kotlin:

classWidgetClass: AppWidgetProvider() {

    overridefunonUpdate(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetIds: IntArray?) {

        for (id in appWidgetIds!!) {
            //get widget options for later get widget dimensionsval options = appWidgetManager?.getAppWidgetOptions(id)
            //get widget view based on widget sizeval view = getView(context, options)
            //update widget
            appWidgetManager!!.updateAppWidget(id, view)
        }

    }

    //listen for widget changesoverridefunonAppWidgetOptionsChanged(context: Context?, appWidgetManager: AppWidgetManager?,
                                           appWidgetId: Int, newOptions: Bundle?) {

        //update widget view based on new options
        appWidgetManager?.updateAppWidget(appWidgetId, getView(context, newOptions))

        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions)
    }

    privatefungetView(context: Context?, options: Bundle?): RemoteViews {

        val minWidth: Intval minHeight: Intif (context!!.resources.configuration.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            || context.resources.configuration.orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {

            minWidth = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) ?: 0
            minHeight = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) ?: 0

        } else {

            minWidth = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH) ?: 0
            minHeight = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) ?: 0

        }

        //get widget view accordin widget sizereturnif (minWidth >= 240)
            RemoteViews(context.packageName, R.layout.widget_large)
        else
            RemoteViews(context.packageName, R.layout.widget_small)
    }
}

Post a Comment for "Dynamically Adjusting Widget's Content And Layout To The Size The User Defined Through Resize. Android"