2

I would like to know if it is possible to add a textview programmatically in a custom view that herites from View. In this customView, I draw some shapes with a canvas and I need to put some text too. I tried to use drawText but it doesn't offer enough possiblities, especially for the position of the text. Basically, i want to put text inside, and precisely in the middle of a circle and at the same time that my text fits right in the circle (I already know how to do that).

That's why I'm wondering if it is possible to add a textview by just declaring it and draw it. Maybe I need to use an Inflater ?

I don't really know what are the best choices so I need your help :)

2 Answers2

3

You can use drawText(String text, float x, float y, Paint paint) method for this. Like this :

Paint mPaint = new Paint();

mPaint.setColor(Color.BLACK);

mPaint.setAntiAlias(true);

mPaint.setStyle(Paint.Style.FILL);

canvas.drawText("YOUR TEXT HERE", 10, 20, mPaint);

Aakanksha
  • 119
  • 1
  • 10
1

Instead of extending View you should extend a ViewGroup subclass like FrameLayout.

This way you can still use the canvas to draw your custom view, but also add children views to your custom drawn viewgroup.

Plato
  • 2,338
  • 18
  • 21