I have a widget with 2 oval buttons (defined in a XML file). At the moment, I've hard coded the width and height of the button to 180dp, but this poses problems when making the widget smaller.
Is there anyway I can set the width and height of the button to scale as the widget size is changed?
Resizable widgets are preferred feature for collection widgets
such as those based on ListView or GridView. App Widget Design Guideline
So for your purpose it's better to use different layout if your widget gets resized. On Android 4.1 and above you may use onAppWidgetOptionsChanged() method to load different layout when your widget is resized. See these answers for more information:
How to use onAppWidgetOptionsChanged() in a widget?
define widget's behaviour/layout for resize and orientation change
Related
In android 4.2, there is a new callback called onWidgetOptionChanged() in which a bundle containing the widget size information is passed back.
http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider
My question is, how do we get the widget size prior to android 4.2?
I need to set a picture to an ImageView in the widget but I need to know the size of the ImageView / Widget to scale the picture accordingly.
Also, the layout used for the widget changes depending on its size. Without onWidgetOptionChanged(), how does one know when to use a different layout depending on the size of the widget?
I'm afraid that you cannot get current widget size on android before 4.2. Only preconfigured widget size through android.appwidget.AppWidgetManager#getAppWidgetInfo.
There are applications that offer the ability to change the font size of a text in a home screen widget. One example is https://play.google.com/store/apps/details?id=org.zooper.zwfree
However home screen widgets only can carry RemoteViews so setting the textSize of a TextView dynamically will not work.
As I see it there are two possibilities to change the text size dynamically:
Add for every text size another layout.xml file. Those files merely differ in the TextView's textSize value. When the user wants to change the textsize, the respective layout has to be loaded.
Draw a Bitmap instead of creating a View like here https://stackoverflow.com/a/4411060/883083
My question is: Is there a third possibility left?
If you're targeting API level 16 or above, you can try the following:
Sadly the whole thing depends on knowing the widget size, which is only possible in API 16+.
Override the AppWidgetProvider.onAppWidgetOptionsChanged callback
or get the same Bundle later via AppWidgetManager.getAppWidgetOptions
Extract the size of the widget:
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
Deduce your TextView's width from the widget size
(best if you have it match_parent to the root of the widget, mind margins/paddings)
If you have complex layout you can alternatively
inflate the whole widget in your app space
widget = LayoutInflater.from(context).inflate(R.layout.widget, null)
Simulate a layout based on the framework:
widget.measure(MeasureSpec.makeMeasureSpec(widgetWidth, MeasureSpec.EXACTLY), ...).
Get your TextView's size: widget.findViewById(R.id.myText).getMeasuredWidth()
Use something like refitText here to find your optimal size
Set the calculated size via RemoteViews.setTextViewTextSize
Note: I didn't implement this method, just thought about it.
Try not to do this on every update, cache the results (even in preferences), widget options shouldn't change often.
I have a ListView inside an AppWidget. Each row of the ListView has a custom layout, AppWidget specifies a minHeight, as required. The problem is that ListView exceeds the dimensions of the AppWidget, and some rows simply hang below the widget boundaries.
I have tried everything I could think of, fixing the height of ListView, using match_parent and/or wrap_content or rows and ListView, but I have not been able to fix this.
Has someone else faced this problem? How do I make the ListView fit inside the widget boundaries and scroll, instead of rows getting outside the boundary?
Well I have root-caused the issue, and I am to blame here, at least partly.
I am asked to use a custom background for the ListView, and the 9-patch I received had a shadow drawn below the 'content' background. Android used the whole png for drawing the background, lending an appearance of 'one row hanging off the content' appearance to the widget.
I verified this by using a solid color as background - it extends properly to cover entire ListView.
My learning:
1. The minWidth and minHeight attributes for the widget are exactly what they say; Android WILL give more space to the widget if it can. :)
2. You've specified "minimum" width, and your actual width is being determined by the width of a child View. Given that such a case could be common, setting the top-level layout width and height to wrap_content in the widget's main view seems a reasonable thing to do.
I'm trying to use a custom font with the text in my Widget. Since the RemoveViews is very limited and doesn't support custom fonts in the TextView options, I found this post that showed a clever way of getting around this. Essentially, instead of using a TextView I use an ImageView and render a bitmap of the text in the custom font and set the ImageView's bitmap to the rendered image. Now the only problem is that I need to determine the width to render the Bitmap so it fits perfect in the fill_parent on the ImageView. So how do I get the width of a widget?
I'm trying to use a custom font with the text in my Widget.
Note that the correct term is "app widgets". Widgets are subclasses of View. App widgets are the things that go on the home screen. See: http://developer.android.com/guide/topics/appwidgets/index.html
So how do I get the width of a widget?
You don't. You know what you asked for via your metadata. What you wind up with is up to the home screen implementation, and there is no way to retrieve that information. That goes double for widgets that the user resizes, either because you indicated that you support resizing, or the home screen just decided to roll that feature themselves.
I have a 4x1 widget that holds several ImageView views inside a LinearLayout. I want to be able to have. I want to be able to adjust the width of those views depending on the screen resolution of the device where the app is installed. My goal is to have each view be the same width, and to have them uniformly distributed across the width of the widget. And I'd like it to fill up the whole width of the widget (so that if the widget covers more space on high resolution devices, each ImageView would be wider). I don't want to create different XML layouts for each device type (mdpi, hdpi, xhdpi, etc) -- I'd like to have this done programmatically when the widget is set up (or updated). But I can't find any documentation in RemoteViews that allows me to change the width of a view. So is there a way to do this?
It's not necessary to do it at runtime.
You can use the android:layout_weight tag in your XML file. If you set the weight to 1 and set each image to 0dp, the images will be the exact same width. That way you won't need to create different layouts for each screen size.