Rajesh
Rajesh

Reputation: 261

How to Draw TextView on Canvas in android..?

How to Draw TextView on Canvas in android..?

We have Canvas.DrawBitmap(), Canvas.drawText(). Do we have any Method in Canvas which takes TextView as a parameter or any other method to display TextView on Canvas?

Actually, I have a alphabet in TextView and I have to make drawing on that alphabet which is in canvas.

Please suggest anything....Thanks for your cooperation

Upvotes: 26

Views: 34285

Answers (3)

Ravi Bhojani
Ravi Bhojani

Reputation: 1052

You need to create a class which will extend Textview. After that override onDraw method. This method provides you to draw your textview tha way you like it

Upvotes: 0

emidander
emidander

Reputation: 2403

You can't draw a Textview directly, but you can put it in a layout and draw the layout. Something like this:

LinearLayout layout = new LinearLayout(context);

TextView textView = new TextView(context); 
textView.setVisibility(View.VISIBLE);
textView.setText("Hello world");
layout.addView(textView);

layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());

// To place the text view somewhere specific:
//canvas.translate(0, 0);

layout.draw(canvas);

Upvotes: 56

Ilya Izhovkin
Ilya Izhovkin

Reputation: 3455

May be you need to use StaticLayout. It can draw formatted text, manages word wrapping and so on. Have a look at http://developer.android.com/reference/android/text/StaticLayout.html

Upvotes: 3

Related Questions