0

Romain Guy created a great presentation and sample implementation for a FlowLayout in Android Activities (which he links to in the answer to this question: How can I do something like a FlowLayout in Android? )

I would love to use something like this in an App Widget but they seem to be extremely locked down to the layout classes mentioned in the documentation: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout .

I tried to extend LinearLayout but it appears descendants of the mentioned layouts are not permitted. Perhaps I can programatically rearrange my TextViews using a RelativeLayout?

Is there a smarter way to achieve this?

Why are Widget layouts so very restrictive? Am I missing something important?

Community
  • 1
  • 1

1 Answers1

2

I tried to extend LinearLayout but it appears descendants of the mentioned layouts are not permitted.

Correct.

Perhaps I can programatically rearrange my TextViews using a RelativeLayout?

I don't see how, as you are not the one rendering your UI.

Is there a smarter way to achieve this?

It is unclear what "this" is. If "this" is "a FlowLayout type UI for an app widget", either write your own home screen (so you can have the UI you want without the need for an app widget), or redesign the UI. App widgets have a limited palette of available widgets and containers, as you have noted.

Strategically, you could contribute a FlowLayout to AOSP and home that it becomes available in some future version of Android, and further hope that it will be enabled for use by app widgets.

Why are Widget layouts so very restrictive? Am I missing something important?

App widgets are rendered by the home screen. There are thousands of home screen implementations. They run in their own processes, with their own code, written by their own developers.

As such, you are not actually creating widgets in your app that are displayed by the home screen. Instead, you are creating a data structure, describing the widgets you want the home screen to create on your behalf. As such, RemoteViews does not support app-defined subclasses, simply because your app's classes are not in the home screen's process.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • As usual, great answer, thanks a lot ! This is actually the best description of RemoteViews I have encountered. – 2Dee Nov 15 '13 at 23:09
  • Thanks. A home screen will not really work for me as I want a widget that works on any home screen the user might be using. I can add TextView objects from xml as RemoteViews to my widget layout. This can be to a RelativeLayout. What I haven't figured out is how to specify the android:layout_toRightOf and layout_below parameter when adding such a view. Perhaps I will just have a static grid of TextViews in my xml and decide which ones to put my text in at runtime. – richardeigenmann Nov 15 '13 at 23:47
  • @richardeigenmann: "What I haven't figured out is how to specify the android:layout_toRightOf and layout_below parameter when adding such a view" -- the problem is that the right answer, for a flow, would depend upon text size and the width. You have decent control over the text size, but you have less control, or even necessarily knowledge of, the width. – CommonsWare Nov 16 '13 at 00:12
  • By counting the characters I'm adding to the TextViews I could get a rough indication of when it's time to do the RelativeLayout equivalent of a CR/LF. Another idea might be to render a bitmap. This would give access to all the text drawing information. – richardeigenmann Nov 16 '13 at 09:26
  • 1
    @richardeigenmann: Be careful with the bitmap approach, as there is a 1MB transaction limit on an IPC call like sending over a `RemoteViews`. – CommonsWare Nov 16 '13 at 11:54