Android Question Allow configuration before adding a widget to homescreen

ducphu

Active Member
Licensed User
Longtime User
Hi all,

Using HD widgets app from playstore as an example, what I want to achieve is that when user click to add a widget, an activity is shown to allow user to configure e.g. background, colors, etc... A widget is only added to homescreen after user click on "OK" button. How can I do it with b4a? Thanks
 

ducphu

Active Member
Licensed User
Longtime User
Hi Erel, can you give an example. Here is my code, how can I implement a check?

B4X:
Sub Service_Create
    rv = ConfigureHomeWidget("widget2x2", "rv", 1440, "Testing")
End Sub

Sub Service_Start (StartingIntent As Intent)
   
    ' If a widget is removed from homescreen we don't need to do anything
    If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED" Then Return
   
    ' We have to be sure that we do not start the service
    ' again if all widgets are removed from homescreen
    If StartingIntent.Action <> "android.appwidget.action.APPWIDGET_DISABLED" Then
        Dim schedule As Long
        Dim now As Long = DateTime.now
        schedule = DateUtils.SetDateAndTime(DateTime.GetYear(now), DateTime.GetMonth(now), DateTime.GetDayOfMonth(now),0,5,0)
        If schedule < now Then
           'skip to tomorrow
           Dim p As Period
           p.Days = 1
           schedule = DateUtils.AddPeriod(schedule, p)
        End If
        StartServiceAt("",schedule,True)
    End If
   
    ' Handle the widget events
    If rv.HandleWidgetEvents(StartingIntent) = False Then
        ' If the action is not handled by HandleWidgetEvents() then we
        ' probably were called by StartService() or StartServiceAt().
        ' So just update the widget.
        rv_RequestUpdate
    End If
   
End Sub
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Hi Erel,

I do not save the configuration in any file. I just want to call an activity when user click on "add widget". In that activity, user can configure anything from text color, text size, background.... If user press "OK" then a widget will be added. If user press "Cancel" then it just cancel everything, no widget is added.
Forgive me but I really new in doing with widget. Can you give me any example, code?
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
OK, let say I store a key in a configuration file. But my first and most important problem is that I'm unable to call up the activity before the widget is added. Can you show me some code which help me to do that please.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You can not show any activity at this point!!

You just can use the service start to do the check.
If the requirements are not ok then you can call a activity to do the settings.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Configuring a homescreen widget is not a simple task with B4A, though it is possible.

Because B4A does not support more than one instance of the same homescreen widget directly you will have to do many "tricks"

You can find a working example of a configurable widget in this tutorial. I think the example should still compile without problems.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
You can start the activity when the service is created. It will happen when the user adds the first widget.

Just call StartActivity(Options)

But this will open the activity on every device reboot, too, I think. (when the widget is on the homescreen of course)
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Rx
Configuring a homescreen widget is not a simple task with B4A, though it is possible.

Because B4A does not support more than one instance of the same homescreen widget directly you will have to do many "tricks"

You can find a working example of a configurable widget in this tutorial. I think the example should still compile without problems.

Thank corwin, I just have a quick look at your tutorial. Look promising and similar to what I want.


@DonManfred don't be so harsh. I know I must write my own code but as I said I'm new to widget and this is not simple so I really need some demo or tutorial.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But this will open the activity on every device reboot, too, I think. (when the widget is on the homescreen of course)
The complete answer is that you need to check whether the widget was configured in the past by checking whether the configuration file exists (or one of the keys with KVS). Only if it wasn't configured then you need to start the activity.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
don't be so harsh
Sorry if you feel like this; but honestly, Erel gave you all Informations on what you need to do. In my eyes no need to ask for an examplecode.

@corwin42 thanks for the link to the tutorial... I Missed that - great - one :)
 
Upvote 0
Top