hoss24
hoss24

Reputation: 556

How do I draw a EditText to canvas in android?

I would like to draw

EditText username = new EditText(context);

to a specific spot on my canvas in

protected void onDraw(Canvas canvas) {

}

Is it possible to draw it on the basis of x,y coordinate in my Java file without using XML layout?

Upvotes: 3

Views: 6996

Answers (2)

M-Wajeeh
M-Wajeeh

Reputation: 17304

Yes you can draw EditText on Canvas, Here is hint:

EditText ed;
.
.
.
.
.   
ed.setDrawingCacheEnabled(true);
Bitmap b = ed.getDrawingCache();
canvas.drawBitmap(bitmap, l, t, r, b, null);

You can create/initialize EditText at run time like this:

EditText editText = new EditText(this);
editText.setText("My Text");
editText.setWidth(180);         
editText.setBackgroundColor(Color.WHITE);

Upvotes: 3

Codeman
Codeman

Reputation: 12375

You can't draw an EditText to canvas in Android. That's not what Canvas is for.

What you can do is use a FrameLayout, put the Canvas inside of it, and put and EditText on top of the Canvas.

Upvotes: -2

Related Questions