Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Example provided in the design guide: Example image provided in the design guide.

But I do not see anything in the docs as to how to accomplish this. How do we change the layout as per resize operation? Any ideas regarding the approach will be appreciated.

like image 474
Gautham Avatar asked Jan 11 '13 01:01

Gautham


People also ask

What is a widget and what does it do?

Widgets can be added to your phone's home as a quick way to access certain information from apps without having to open the app itself. One example is the Calendar widget, which provides a quick view of the upcoming events in your calendar without having to open the Calendar application.

What are widgets available in Android explain any two widgets with example?

There are given a lot of android widgets with simplified examples such as Button, EditText, AutoCompleteTextView, ToggleButton, DatePicker, TimePicker, ProgressBar etc. Let's learn how to perform event handling on button click. Displays information for the short duration of time.

What is the meaning of widget in Android?

Widgets are an essential aspect of home screen customization. You can imagine them as "at-a-glance" views of an app's most important data and functionality that is accessible right from the user's home screen.

Which function returns the height of a widget in Android?

To get the size/position of a widget on screen, you can use GlobalKey to get its BuildContext to then find the RenderBox of that specific widget, which will contain its global position and rendered size.


1 Answers

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) @Override public void onAppWidgetOptionsChanged(Context context,         AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {      Log.d(DEBUG_TAG, "Changed dimensions");      // See the dimensions and     Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);      // Get min width and height.     int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);     int minHeight = 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.     int rows = getCellsForSize(minHeight);     int columns = getCellsForSize(minWidth);      if (columns == 4) {         // Get 4 column widget remote view and return     } else {                     // Get appropriate remote view.         return new RemoteViews(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.  */  private static int getCellsForSize(int size) {   int n = 2;   while (70 * n - 30 < size) {     ++n;   }   return n - 1;  } 
like image 127
Gautham Avatar answered Oct 05 '22 23:10

Gautham