Text Drawables

Nathan Sokalski 4,116 Reputation points
2022-05-15T22:50:21.493+00:00

Is there any kind of Drawable that allows me to display text? I have a scenario that requires a Drawable, and I want to display text (actually, just a single character). I was hoping to do this with some kind of built-in Drawable type, but the best alternative I can find right now is to use a Vector to make something that looks like the desired character. Any ideas?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Rob Caplan - MSFT 5,412 Reputation points Microsoft Employee
    2022-05-16T00:50:24.963+00:00

    Please include the target versions that you're using. I assume from "Drawable" that you're targeting Xamarin.Android native UI and not iOS or Forms, but it's clearer if you're explicit about that.

    Xamarin.Android is a light wrapper around the Android API, so you have access to and the same capabilities as a native Android app. Android doesn't provide a TextDrawable, so the common pattern is to create a custom TextDrawable by inheriting from Drawable and handling the Draw method to call Canvas.DrawText.

    There is an example of handling OnDraw to draw with a Canvas in the Xamarin Android Graphics and Animation documentation.

    There are a number of native Android implementations you can look at that should be easy to port to C#, as well as at least one C# implementation, though I don't know if it's current enough to work as is.