user446010
user446010

Reputation: 79

TextView on Canvas

Is it possible to add TextView and ImageView on canvas?

Upvotes: 1

Views: 9125

Answers (4)

Engin OZTURK
Engin OZTURK

Reputation: 2125

- put your view (which is using canvas) inside the RelativeLayout,

public class YourComponent extends View{
@Override
protected void onDraw(Canvas canvas)

...

public void onDraw(){
....
....
ImageView yourImageView = .....

RelativeLayout.LayoutParams fParams = new RelativeLayout.LayoutParams(25,25);
fParams.leftMargin = 100; //x coordinate
fParams.topMargin  = 25;  /y coordinate
yourImageView.setLayoutParams(fParams);
((RelativeLayout) YourComponent.this.getParent()).addView(yourImageView);

}

}

above code : i created layout params which shows 25x25 dimensions for view. and after that , i used margins to set proper position on the parent layout. and set this LayoutParams to ImageView. and for last, i added this imageView to parent Layout.

But beware that during the onDraw() execution, calling operation on external view may stop onDraw(). so if you want to use this way be sure you already completed all onDraw operation. I am not so sure why this happens. I did not try to observe too much on it yet.

Upvotes: 0

bryce
bryce

Reputation: 706

It's a round about way, but this tutorial adds a textview to his canvas:

http://www.helloandroid.com/tutorials/how-draw-multiline-text-canvas-easily

Upvotes: 2

DavGin
DavGin

Reputation: 8355

If you need to freely position ImageViews and TextViews, you should use a RelativeLayout.

The RelativeLayout has advanced positioning features and allows overlapping.

Upvotes: 0

Rich
Rich

Reputation: 36806

Canvas does not inherit from ViewGroup, so it does not have the ability to add child views to it.

With Canvas, you use drawBitmap and drawText methods to draw images and text instead of adding child controls like TextView and ImageView.

Upvotes: 2

Related Questions