Android Question Custom font on a widget view

Discussion in 'Android Questions' started by Paulo Rosa, Aug 12, 2019.

  1. Paulo Rosa

    Paulo Rosa Member Licensed User

  2. Peter Simpson

    Peter Simpson Expert Licensed User

    Hey @Paulo Rosa,

    The only way I know how to do what you are looking for (which is obviously not the only way to do it) is to use Canvas.DrawText to create a TextBitmap, then use the Bitmap on your widget by using RV.SetImage.

    Something like this, this is just the basics on how I would do it.
    Code:
    'CODE NOT TESTED

        
    Dim BitmapTxtToImg As Bitmap
            BitmapTxtToImg.InitializeMutable(Width, Height)

        
    Dim CustomFont As Typeface = Typeface.LoadFromAssets(<YourFont>)
        
    Dim CanvasImage As Canvas
            CanvasImage.Initialize2(BitmapTxtToImg)

        CanvasImage.DrawText(
    "Hello World"10dip10dip, CustomFont, 28Colors.RGB(000), "CENTER")

        RV.SetImage(<ImageviewOnYourWidget>, CanvasImage.Bitmap)
        RV.UpdateWidget
    I believe that your ImageView on your widget should now say 'Hello World'.

    As I said previously, there are multiple ways to achieve your results, this is how I would do it rightly or wrongly. I'm sure that someone else will give you another method.

    Enjoy...
     
    Last edited: Aug 12, 2019
    Erel likes this.
  3. Paulo Rosa

    Paulo Rosa Member Licensed User

    Thanks, I'll try that.
     
  4. Paulo Rosa

    Paulo Rosa Member Licensed User

    I wonder if that code would significantly affect battery lifetime.

    Let me explain...

    I'm building a very simple app, it's a clock widget. The widget uses a Broadcast Receiver to capture the intent "android.intent.action.TIME_TICK", which occurs every minute. So, every minute my code runs and displays the correct time. Currently the time is shown on a label with standard typeface (DEFAULT), so the processor overhead is minimum, just a pair of code lines every minute to change the label text.

    Regards,
     
  5. Peter Simpson

    Peter Simpson Expert Licensed User

    There's only one way to find out...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice