I have a customview looking like this:
public class CustomView extends View {
protected Context c;
protected String text;
... // and some more useful member variables...
public CustomView(String text, Context c, ...) {
this.text = text; this.c = c;
...
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
LinearLayout ll = new LinearLayout(c);
TextView tv = new TextView(c);
tv.setText(text);
ll.addView(tv);
ll.draw(canvas);
}
And in my main activity, I do this:
RelativeLayout gamelayout = (RelativeLayout) findViewById(R.id.gamelayout);
CustomView customview = new CustomView("Textview text", this);
gamelayout.addView(customview);
My problem is, that simply nothing is drawn, the drawn TextView does not appear in the “gamelayout”. What am I doing wrong?
TextView objects are not able to draw directly to the canvas, as you’ve done you need to assign to a Layout and then do this:
Personally, I’m surprised there aren’t errors thrown for calling
ll.draw(). Unless you have a need to drawing a TextView I prefer drawing the text to the canvas:See documentation here