Change Fonts in Widget

squaremation

Active Member
Licensed User
Longtime User
There is a work around using the Android SDK, by rendering the font onto a canvas, and passing it on to a bitmap and assign that to an imageview. Anyone got a idea on how to accomplish this in B4E?
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Haven't tried it myself, but that's basically how to render on a bitmap:

B4X:
Sub DrawFont(MyText As String) As Bitmap
   Dim MyFont As Typeface
   MyFont = Typeface.LoadFromAssets("myfont.ttf")
   Dim Canvas As Canvas
   Dim imgCC As Bitmap
   imgCC.InitializeMutable(40dip,16dip) 'sample size
   Canvas.Initialize2(imgCC)
   Canvas.DrawText(MyText, 0, 0, MyFont, 16, Colors.Blue, "LEFT")   
   Return Canvas.Bitmap
End Sub

Please place the Sub in a code module, not in the widget service and call it from the widget service:

rv.SetImage("imvFonts",[code module name].DrawFont("New font"))

Personally, I would not compromise simplicity and use the Android fonts.
 
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
Thanks this does work (in Digital Clock) but FC's :( , I believe when widget updates.

I will take your advice at this point
Personally, I would not compromise simplicity and use the Android fonts.

I'll revisit this concept at a later date with more time to dedicate to it
 
Upvote 0

satomware

Member
Licensed User
Longtime User
Hi,

I'm looking for a solution to the same problem... I need to change fonts in my widget, but unfortunately this workaround doesn't seem to work for me (nothing appears in the widget), and it also causes the widget to FC, as squaremation said.

Any news about this thread? :sign0163:

Thank you!
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
My widget with 3 custom fonts works perfect with the code in this post, which was basically the same as my own code.

I also followed Erels tutorial, everything works perfect.

The only problem I've found is that in Android 2.2/2.3, my widgets memory using just keeps going up, in Android 4.0 the memory usage stays low. I'm presuming that the garbage collection is managed better by Android 4.0. I have not tested it in Android 3.2. I've tried clearing the bitmap image after using the image but with no joy, I'm also not able to release bitmap resource afterwards.

uploadfromtaptalk1341598008025.jpg

Sent from my A500 using Tapatalk 2
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I don't get an 'out of memory error'.......

Hello,
I don't get an 'out of memory error', but my widget service memory usage does go up by about 20mb-25mb every 24 hours, that's if I'm using a widget layout with custom fonts. My widget starts of at 5mb in size(I'm using a few libraries).

I've managed to slow down the memory usage by only creating text to image when it actually needs to and not every second. I also monitor if the screen is actually on. If the screen is not on then my widget skips the text to image routine until the screen is back on again. I integrated the screen on routine into a one second timer which I already had running in my widget. But this still does not fix the underline issue, just slows it down.

I would love to be able to keep the memory usage down to a minimum(lets say 8mb-10mb).

Anyway, I'm happy with the result so far. As after about 30 hours or so the garbage collection does its stuff and my widget memory usage goes down to 5mb again.
 

Attachments

  • ConPan3.jpg
    ConPan3.jpg
    7.2 KB · Views: 234
Last edited:
Upvote 0

satomware

Member
Licensed User
Longtime User
Usually if you don't get an out of memory error then everything is fine. The garbage collector doesn't immediately releases the memory. If there is a real memory leak then eventually you will get an error.

Yes Erel, but for the final user it's not good to see a simple widget that uses more than 50mb of RAM... Even without an out of memory error!
 
Upvote 0
Top