In my app I have a 4x2 Widget and now I wanted a second 4x1 Widget displayig almost the same content, so I created a secondary appwidget-provider xml. When I then wanted to use the same Widgetprovider Android only showed one widget in the list. :-(
As I found out, you have to use for each Widget defined in the Manifest another widgetprovider... why?
how to add multiple widgets in one app?
Furthermore I would liek to have only one widget entry in the List...
Can I change the appwidget-provider xml in the AppWidgetConfig Activity so that I don't have to use the initial size? how?
you have to use for each Widget defined in the Manifest another widgetprovider... why?
Because that is the way they wrote it.
Furthermore I would liek to have only one widget entry in the List
Then only define one AppWidgetProvider for only one size of app widget.
Can I change the appwidget-provider xml in the AppWidgetConfig Activity so that I don't have to use the initial size?
No.
Related
I have some widgets for my app who do the same thing just a different in their layout causing a lot of duplicate code in multiple AppWidgetProviders, creating multiple of basically the same appwidget-provider xml files and filing up my manifest with all these different receivers.
Is there a way to setup widgets to use the same provider but with different layouts? As far as I understand each widget needs its own receiver in the manifest to show up
Android supported loading different layout.set RemoteViews different is OK?
Right now I am in the process of creating a custom date picker on screen in a test app, but I wish to use it in multiple activities in my app, and it is complex enough that I just don't want to copy and past the code. I was wondering whether there is a way to nicely package it so I can reuse it easily.
Thanks for any help and info!
Create whatever widget u intend to in a separate layout resource say "my_widget. xml"
And then use it another activity's xml (say activity_1.xml) by calling your widget in it, using
<include layout=#layout/my_widget />
Create a custom view (aka widget) following this "Guide" implementing all your customizations you currently have to the view in your activity (as I understood) and then you can reuse your widget anywhere in this application or even on other applications (this is if you export this view into separate lib, and is subject for another discussion)
I was searching in internet but nothing helps. I want to make second widget, just bigger. Im using service to update my widget so I need to create 2 services (update data in widget by remoteviews etc.) or can I make it easier?
When I add medium widget, it shows up te small one but it takes more space (144dp x 72dp) and there is no update. Maybe I should copy whole app classes and modify it, but I think this is stupid solution.
I was trying this solutions
how to add multiple widgets in one app?
How to put multiple widget sizes in one apk?
Any hints? :)
I had the same need with my widget. Precisely those two links you have posted helped me find the solution, which came at last with a bit more of research.
In order to have a second size for your widget:
Define another receiver in your manifest, with a diferent name. This is important: if you don't use a different name, only one will show in the list of available widgets. For example: android:name=".MyWidget_Small" and android:name=".MyWidget_Medium". Also modify the labels accordingly
The additional receiver needs a new appwidget provider in res/xml. In android:resource of the meta-data tag type the name of another XML file. You'll have then android:resource="#xml/MyWidgetProvider_Small" and android:resource="#xml/MyWidgetProvider_Medium"
Each XML file in res/xml must point to a separate layout. You will have android:initialLayout="#layout/MyWidget_Small" and android:initialLayout="#layout/MyWidget_Medium"
Now the Java part. Each appwidget provider need its correspondent Java class that extends AppWidgetProvider, so you will need two additional classes. If your original class was MyWidget.java, you'll add MyWidgetSmall.java and MyWidgetMedium.java. You'll probably end up subclassing your original Java class, and perhaps modifing each new class if the widget has a distinct behavior in each size. Remember to name the two classes as you did in step 1 in the receivers's android:name
Your service doesn't need duplication. However you should examine your source code to find occurrences of explicit references to MyWidget.class. If you did this, you must reference the actual subclass
Hope this helps
Is it possible to create a Home Screen widget dynamically (that is without defining a fixed layout in project resources)?
Currently I have 4 layouts - but to cover all options I'll need at least 15 different layouts with different width / height which fills up the widget selector for user.
I'd prefer to offer the widget design in App preferences then create layout as required.
You can't change the widget size, unless you are on a tablet which is running Android 3.1 (Honeycomb). Otherwise you have to specify all the sizes you need as a single widget.
Regarding all the different layouts and the filled widget selector. You only have to add one entry there for every size. After the user clicks on one of these entries,
you can implement a configuration/select-activity (e.g. like you can see in sense, or the News&Weather app from google where you pick a layout with weather, news or both).
The user can pick a layout there for the widget. You just have to remember that choice and
set the correct layout when the widget is updating. You can change the widgets layout
inside the RemoveViews constructor that you have inside a widget update function anyway:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
What I'm trying to do is have a clock widget of different sizes (i.e. 2x2, 3x3, 4x4 etc) in one apk and a configuration activity to be able to select which size to add.
From what i've learned from documentation:
Widget size is specified in <appwidget-provider> tag in respective xml file
Also in that file I set up the configuration activity for that provider
So it seems that size is a property of AppWidgetProvider and I'll need to somehow create another provider from the code in configuration Activity of the first one...
Or am I getting this wrong and there's another way?
Is this possible at all? :)
I've been told that some widgets can do this :)
Thanks in advance!
P.S. I have read this and this. First one explains how to put multiple wigets in one apk, but it's not clear how to select between them in runtime. Second one is about changing layouts, but not size...
Do you really have to do this at runtime? It is possible to define several widgets in one .apk.
Maybe you can have a look on transdroid source? They are including two sizes of a widget in the same package.
AndroidManifest.xml used by Transdroid
I hope I have read your question right and think I have your answer. I could rewrite this information or just point you to it (as I would more than likely not be able to do it justice).
http://developer.android.com/guide/topics/appwidgets/index.html#Manifest
Look under "Creating an App Widget Configuration Activity". The example is good as well.
By doing it this way you may not be able to set a certain layout sizes without a lot of editing but will allow for customization by the user (which is what you want correct?)
I have the same problem and was hoping to find a way to add only a single entry to the Widget list, and have the configuration activity decide the size, but I realize this is not possible.
When thinking about it, it's only fair: if the program would have the right to set the widget's size at runtime, one would maliciously increase the widget once added to the launcher, and occupy more real estate than it was authorized to.
So as mentioned above, the solution is to add multiple widget providers and let the user choose the size she wants. You can even use the same configuration Activity if the options are not related to style but only colors, or refresh periodicity.