Is it possible to add TextView
and ImageView
on canvas?

- 5,965
- 14
- 31
- 57

- 79
- 1
- 2
- 9
-
This shows the way too.. [http://stackoverflow.com/questions/5242757/how-to-draw-textview-on-canvas-in-android][1] [1]: http://stackoverflow.com/questions/5242757/how-to-draw-textview-on-canvas-in-android – Rukmal Dias Dec 17 '12 at 07:18
4 Answers
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.

- 36,270
- 31
- 115
- 154
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

- 698
- 10
- 11
- 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.

- 2,065
- 2
- 21
- 13
If you need to freely position ImageViews and TextViews, you should use a RelativeLayout.
The RelativeLayout has advanced positioning features and allows overlapping.

- 8,205
- 2
- 19
- 26