Android Question Canvas on widget not properly displayed

ducphu

Active Member
Licensed User
Hi all,

I use canvas to drawtext on a imageview in my widget. The problem is, if I set the text to draw as a fixed one, e.g "A", it works. But if I set the text to draw as a variable, when I add the widget to homescreen, I may or may not display the text. Below is the code and screenshoot. The screenshoot is when I continuously add 4 widgets. 2 of them show the text and 2 doesn't.

B4X:
Sub rv_RequestUpdate
    DateTime.DateFormat = "dd.MM.yyyy"
    Dim header As String = DateTime.date(DateTime.Now)
    Dim day As Int= DateTime.GetDayOfWeek(DateTime.Now)

    If day = 1 Then header = "Sun, " & header
    If day = 2 Then header = "Mon, " & header
    If day = 3 Then header = "Tue, " & header
    If day = 4 Then header = "Wed, " & header
    If day = 5 Then header = "Thu, " & header
    If day = 6 Then header = "Fri, " & header
    If day = 7 Then header = "Sat, " & header
   
    rv.SetImage("Header",CommonCode.DrawFont(header,"Header"))
    rv.UpdateWidget
End Sub
B4X:
Sub DrawFont(MyText As String, Target As String) As Bitmap
    Dim Font As Typeface
    Dim Canvas As Canvas
    Dim imgCC As Bitmap
    If Target = "Header" Then
        Font = Typeface.LoadFromAssets("CORSIVA.ttf")
        imgCC.InitializeMutable(146dip,30dip) 'sample size
        Canvas.Initialize2(imgCC)
        Canvas.DrawText(MyText, 73dip, 28dip, Font, 20, Colors.White, "CENTER")
    End If
    Return Canvas.Bitmap
End Sub
 

Attachments

ducphu

Active Member
Licensed User
I tried to show the log for every step, right after every rv.SetImage, and rv.UpdateWidget. All the logs are showed so it's supposed that it has run through every step. But the texts are still sometimes appeared and sometimes not. :confused:

P.S and Erel was right. Even I draw a literal string ("a"), the problem still occurs. Help me :(
 
Last edited:

ducphu

Active Member
Licensed User
Hi Erel,

I think there is bug on the Imageview. If my widget contains Imageview, even if I do not draw anything on it, the problem still happens. Other label's texts are affected and most of the time all the texts are not displayed. The chance for the problem to happen increase with the number of imageviews on the widget. The more imageviews the higher chance that the all the text are not displayed (it doesn't matter whether I draw anything to the imageview or not)

P/S If I only use label with text, this bug still happens but the chance is very low, and only for the first widget added (so far I observed) very weird :( I tested corwin AHClock widget, in which he only uses label and text, same bug happens.
 

Attachments

Last edited:

corwin42

Expert
Licensed User
This is not a bug with B4A but it is a limitation with the way how B4A updates the contents of the ImageView. The problem is that the content for the RemoteViews is "streamed" to the views and this only works with small amount of data because there is only a small buffer for the content.

I solved this for one of my apps by updating the RemoteView with "setImageViewUri". So my app does not send content to the RemoteView but just an URI. Unfortunately a part of this is implemented in a non public custom library. I will see if I can extract it and do the library part with Reflection or JavaObject. It is a bit complicated and you have to store your images in Objects/res/drawable folder.
 

ducphu

Active Member
Licensed User
Hi corwin,
If this is a limitation then it's "really limited". Because I tried with a few widgets, including your AHClock and Homewidget from Erel's tutorial.
For your AHclock, you only use 1 label, no imageview. But I still see this bug happens, even the chance is very low.
For Erel's widget, he uses label and button, again no imageview. Bug happens more often, like 50/50.
SO, for all the widget I tested, the amount of data being streamed is relatively "small". If even with those simple widgets, the streaming can not be done properly then I think it should be a bug rather than a limitation :(
 
Top