0

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?

user3858843
  • 95
  • 1
  • 13

2 Answers2

2
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

MeGoodGuy
  • 395
  • 2
  • 10
  • I'm dumb for not noticing that. That stopped the runtime error. Now I'm having the problem of not seeing anything. Tried setting the editText visibility, enabling the requestFocus, setting a height/position. Nothing I add is making it visible. – user3858843 Dec 01 '15 at 23:18
  • You should read on how views work. Now you should probable accept this answer because your first question is why do you have an exception thrown. –  Dec 01 '15 at 23:23
  • i found something :) u have to put it in a layout http://stackoverflow.com/questions/5242757/how-to-draw-textview-on-canvas-in-android – MeGoodGuy Dec 01 '15 at 23:39
0

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!