Android Question What is best practice now to make a widget ? in B4XPages and B4A 10.6/7

AnandGupta

Expert
Licensed User
Longtime User
All the thread I found in our Forum on widget making are very old.
Many use libraries which are not available or not advised to use.

So, what is best practice now to make a widget ?

Regards,

Anand
 

AnandGupta

Expert
Licensed User
Longtime User
The term "Widget" has now several different meanings.
Sorry for not being clear.

I posted in Android Forum for Android Widget. The one we see in Android phones.

Like below,

Any help is appreciated.

Regards,

Anand
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I have not used this feature, but I am intrigued, others will have a quicker response.
Since it seems possible to draw on canvas in a widget, I will try to implement my Not-So-Simple Clock project.
I'll let you know of my progress.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I just tried the 24-hour clock by @MarkusR. It is dated 2018 and it works great.

 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I just tried the 24-hour clock by @MarkusR. It is dated 2018 and it works great
Yes.

Presently I am playing with below ones, I found in our Forum,









Some work, some don't due to very old libraries required.

Since all are old codes, so I was wondering if there is a better way now in B4XPages logic.

Regards,

Anand
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
From what the tutorial says, the Android Widget is a special service with unique limitations. However the host/parent app can be anything, with or without classes.
So it can use B4XPages. The Widget service is started (passing the intent handler as a parameter) and then the widget event response sends the intend to the host/parent App.

B4X:
Sub Service_Start (StartingIntent As Intent)   
    If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Update. B4XPages produces persistent Apps, so not appropriate to Widgets, where you want the host/parent to go into the background.
You can close the MainPage and that seems to work, but then you might as well use a regular Activity.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
F.Y.I. I started to adapt my Not-So-simple Clock to a widget. There are quite a few obstacles. The primary one is that all the 24 mini clocks run asynchronously, and the activity doesn't have persistent memory between re-activations initiated the Widget. There isn't enough time to animate from files, so the state of the image has to be saved in the Widget itself. Maybe I can do all the animation inside the Widget, without aid of the activity, whose only job would be to start the Widget.

Another problem is that the canvas can not be a global, so it has to be created on the fly, and can't be stored as a property of a class. I'll see what I can do tomorrow.
You may not be too interested in what I'm doing but I thought I'd share my experience with the Forum.
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
You may not be too interested in what I'm doing but I thought I'd share my experience with the Forum.
All efforts are useful and helpful. I may not have this requirement now, but knowledge of it is going to help me in future, I believe.
Thanks for your hard work.

Regards,

Anand
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
You can close the MainPage and that seems to work, but then you might as well use a regular Activity.
Yes this is looking tricky as I will want to open my app, which is now B4XPages, when the widget is touched.

Only @Erel can throw proper light here, I think.

Regards,

Anand
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Starting an activity from a service is more or less not possible in Android 10 (it is possible with a special permission).
Thanks for the reply.

So, taking the "widget tutorial 2" example, if I want the user to touch the widget to open a dialog box where he/she can change the url for quotes (for example), how can I implement the same ? (I have seen apps doing that)

Regards,

Anand
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Try to call StartActivity(Main) on an Android 10+. According to Android docs it will not work, but it is worth a test.
Sorry for the intrusion, but I have scared myself when reading that.
What should be used then on Android V10? :oops:
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Sorry for the intrusion, but I have scared myself when reading that.
What should be used then on Android V10? :oops:
I do not have Android v10 or above, so I have not tested it.

You can check any existing widget code from Forum in your Android v10 or above and revert. It will help make further discussion on the outcome.

Regards,

Anand
 
Upvote 0
Top