Android Question Does widget need to be in a standalone project?

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

While trying to add a widget to a project, I ran into an odd issue where my widget would not show up in the list of widgets. My existing project has 3 activities and a service. I added the widget service as advised in tutorials in the forum, but could not see my widget show up.

Then I create a new B4A project and created the same widget service and layout. Then when I launched this project, I saw my widget listed on the device.

Is there anything extra I need to do to add a widget in an existing project? Or is creating a new widget only project the only way to add widgets? This will be very unusual and as it will require packaging two apps which should only be one app. Hence, I believe I am missing something.

[EDIT] Have also noticed that the only way to debug a widget is to use legacy mode. The fast debug throws exceptions, starting with could not find widgetlayout.bal ...
Thanks.
 

KMatle

Expert
Licensed User
Longtime User
Just added a widget to one of my existing projects. No problems at all.

1. make a new layout with the designer (panel -> all views on this panel)
2. add a service module
3. put the code of the example in it
4. change names, etc.
5. cimpile, install & run

Can you post your service module?
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
Just added a widget to one of my existing projects. No problems at all.

1. make a new layout with the designer (panel -> all views on this panel)
2. add a service module
3. put the code of the example in it
4. change names, etc.
5. cimpile, install & run

Can you post your service module?

Thanks Klaus. But the issue still remains. I have not exactly just copied and pastes the service code but there is no difference from what I see. My layout is different than the example but that shouldn't break anything I'd assume. Following is my widget service module. Just to be sure, my layout's name is indeed WidgetLayout.
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim rv As RemoteViews
End Sub
Sub Service_Create
    'configure the widget and set it to not update automatically.
    rv = ConfigureHomeWidget("WidgetLayout", "rv", 0, "Messaging Assitant", True)
End Sub

Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub
Sub rv_RequestUpdate
    rv.UpdateWidget
End Sub
Sub rv_Disabled
    StopService("")
End Sub

Sub Service_Destroy

End Sub

[UPDATE} In the Log, I can see that this service is never created.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Hm. Do you do something with it? From my point of view it is empty (no code).

I tried this one (with a simple button & label) which works. It puts the actual time in a label. Clicking on the button starts the main activity :)

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim rv As RemoteViews
End Sub
Sub Service_Create
    rv = ConfigureHomeWidget("Widget.bal", "rv", 1, "I'm a widget :-)", True)
 
End Sub

Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub rv_RequestUpdate
    rv.SetText("Widlbl",DateTime.Time(DateTime.Now))
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    StopService("")
End Sub

Sub Service_Destroy

End Sub
Sub Widbtn_Click
StartActivity(Main)
End Sub
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
I'm sorry but I don't quite understand what does it mean when you say 'do you do something with it' :) I just want to show a static widget with preconfigured layout and views. The only thing that I see different from your service is that my service does not cater for button clicks(I can do that later) and that I am not changing anything in the widget views, such as text(i will do this later as well).

The issue I'm facing is that my widget doesn't show up in the widgets list on the emulator. I have checked the manifest and the widget service, the provider info and the layout are all listed properly(of course it is code generated by Erel after all :). So why does it not show in the list of widgets?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
This was just an example which works... Relax :) Just trying to help and I cannot load you app to my brain to test it ;-)

Well, I read emulator? But you did test it on a real device? You could upload your project to have a look at...
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
I am quite relaxed, believe it or not and enjoying my Saturday with B4A :)

I'm attaching my project here. No jokes about bad coding please ;) Am only a beginner student..

BTW, also tried to test on a device but the result is the same. No widget available in the widgets list
 

Attachments

  • MessagingPA.zip
    21.4 KB · Views: 246
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
Well I just created a new project, added the widget service and layout and then imported the existing modules into the project :)
 
Upvote 0
Top