0

When using Activities you can easily select the layout to be used as you wish in the onCreate() via setContentView(). But how do I accomplish this on an app widget? As far as I know the app widget's layout is determined by the corresponding AppWidgetProviderInfo.xml, which isn't configurable at runtime, is it?

So how can I select a specific layout depending on the device's version of Android?

wodzu
  • 3,004
  • 3
  • 25
  • 41

2 Answers2

0

AppWidgetProviderInfo provides the default initial layout, but you can specify a layout at runtime when creating your RemoteViews object for the appwidget.

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
    R.layout.layout_id);

Combine this with checks against the android.os.Build.VERSION.SDK_INT value and that should probably get you where you want to go.

Simes
  • 453
  • 5
  • 8
0

The best way to do this is to use the resource qualifiers in android, the same way you would substitute strings for a different locale or dimension for a different screen size. You can find more about this feature by looking at http://developer.android.com/guide/topics/resources/providing-resources.html#table2

You could use this to define a new widget XML specification in the xml/ directory or an alternative layout in the layout/ directory.

It would look something like this:

  • layout/widget_layout.xml (default or layout for lowest API version)
  • layout_v11/widget_layout.xml (version to use for API level 11)

When you are running API 11 or greater, the layout file in layout_v11 will be used.

Brian Bauman
  • 1,000
  • 1
  • 9
  • 23