question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan commented

Text Drawables

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?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RobCaplan avatar image
0 Votes"
RobCaplan answered RobCaplan commented

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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are correct that I am using Xamarin.Android Native. I have never created a custom Drawable, are there any good examples of doing that? Because a Text Drawable is (I would assume) a not uncommon need, I was wondering if there was anywhere that I could find one that was already done (I know, it makes it sound like I'm trying to be lazy, but I figure there is no reason to redo something that someone else may have already done (probably better than I would end up doing as well)). Either way, are there any good examples of creating custom Drawables? Thanks.

0 Votes 0 ·

Nothing concrete I can point at beyond what I already provided. There are a number of classes which demonstrate overriding OnDraw in the Xamarin monodroid-samples repo, which is all essentially the same as demonstrated in the Xamarin Android Graphics and Animation documentation.

The general idea is fairly simple. You'll just need to concretize it to your specific needs.

0 Votes 0 ·

I have tried making a custom drawable as follows:

 public class TextDrawable : ShapeDrawable
 {
     public override void Draw(Canvas canvas)
     {
         base.Draw(canvas);
         Paint textpaint = new Paint() { Color = Application.Context.Resources.GetColor(Resource.Color.Black, null), TextAlign = Paint.Align.Center, TextSize = 2f * canvas.Height / 3 };
         textpaint.SetTypeface(Application.Context.Resources.GetFont(Resource.Font.arialfamily));
         Rect bounds = new Rect();
         textpaint.GetTextBounds("?", 0, 10, bounds);
         canvas.DrawText("?", canvas.Width / 2f, canvas.Height + bounds.Height() / 2f, textpaint);
     }
 }

However, I'm having trouble adding it as an xml drawable. Here is the current code in the xml file:

 <com.companyname.Kakuro2.textdrawable xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
     <size android:width="20dp" android:height="20dp"/>
     <solid android:color="@color/White"/>
 </com.companyname.Kakuro2.textdrawable>

However, this is causing the following line (in OnCreate) to throw a NullReferenceException:

 SetContentView(Resource.Layout.activity_main);

Is there something wrong with my custom drawable? Is there something wrong with my xml drawable (sometimes I am unsure about capitalization for fully qualified names)?

0 Votes 0 ·
Show more comments