Richard Le Mesurier
Richard Le Mesurier

Reputation: 29724

Android widget max allowed size

We can set the MINIMUM size of a widget using e.g.

android:minHeight="40dp"
android:minResizeHeight="40dp"
android:minResizeWidth="40dp"
android:minWidth="40dp"

There are no android:max___ properties.

Is there a way to limit the MAXIMUM size of a resizeable widget?

(perhaps a resize callback I'm not seeing or something like that?)


Noting that the docs say a common use case for resizing is lists or grids. Which require no setting of a max size.

My use case is simply "client asked for a single widget that is either 1x1 or 2x2". Will he get what he wants, or will I have to change his mind for him?

Upvotes: 4

Views: 6183

Answers (3)

gilgamesh
gilgamesh

Reputation: 11

As @Sami Eltamawy said you can't set maximum height. but you can set at widget layout: layout_height="wrap_content".

With this way, widget size will be limited with your content.

I know that it won't help to OP but it can help others

Upvotes: 1

ampedNinja
ampedNinja

Reputation: 31

Few years late but, maybe doing a second layout where if user crosses a max width/height, the widget will switch to this layout and no longer grow in size to match_parent.

Using a Layout with the predefined max height/width, with the addition of nesting inside a frame layout if you want to control where it sits inside it's bounding area.

Upvotes: 2

Sami Eltamawy
Sami Eltamawy

Reputation: 10009

Is there a way to limit the MAXIMUM size of a resizeable widget?

NO, but you can limit the user by disable the horizontal or vertical re-sizing in the XML file of your widget.

Example: for only Horizontal

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="220dp"
android:minWidth="180dp"
android:resizeMode="horizontal"
android:updatePeriodMillis="0" 
android:previewImage="@drawable/widget_all_in_one_preview"/>

Example: for only Vertical

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="220dp"
android:minWidth="180dp"
android:resizeMode="vertical"
android:updatePeriodMillis="0" 
android:previewImage="@drawable/widget_all_in_one_preview"/>

You can also do both by:

android:resizeMode="horizontal|vertical"

Make sure you are using NinePatch images or setting the Background color programatically.

OR provide a fixed size Widget to be in the safe side :)

I hope it helps

Upvotes: 9

Related Questions