user3858843
user3858843

Reputation: 95

Can't draw editText in android canvas

I am trying to add an editText box to my android project. This project is done almost entirely with Java (barely any xml), so I would like to know how to get this done in Java. My current implementation which is getting a runtime error is as follows:

public class MainMenu extends View{
    EditText editText;

    public MainMenu(Context context) {
        super(context);
        EditText editText = new EditText(context);
        editText.setDrawingCacheEnabled(true);
        editText.setText("My Text");
        editText.setWidth(180);         
        editText.setBackgroundColor(Color.WHITE);
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        editText.draw(canvas);
        invalidate();
    }
}

Can anyone point out what is wrong and possibly offer a solution using Java?

Upvotes: 0

Views: 852

Answers (2)

user5619242
user5619242

Reputation:

I think you should use:

EditText text = (EditText) findViewById(R.id.editTextId);

You can find this in the top-right of the design xml file. Good luck!

Upvotes: 0

MeGoodGuy
MeGoodGuy

Reputation: 435

EditText editText = new EditText(context);

Your "editText" will be null in the onDraw method i think

editText = new EditText(context);

Please let us know when u resolved it

Upvotes: 2

Related Questions