0

My ending objective is to have a simple text input box on the canvas that when clicked the user can enter some number into the box. This needs to be done with Java and not XML.

My code so far looks like this:

public class MainMenu extends View{
    EditText editText;

    public MainMenu(Context context) {
        super(context);
        editText = new EditText(context);
        editText.setDrawingCacheEnabled(true);
        editText.setVisibility(View.VISIBLE);
        editText.setText("My Text");
        editText.setWidth(180);
        editText.setHeight(200);
        editText.setLeft(500);
        editText.setTop(500);
        editText.setBackgroundColor(Color.WHITE);
        editText.requestFocus();
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        editText.draw(canvas);
        invalidate();
    }
}

Does anyone know why my editText won't display?

user3858843
  • 95
  • 1
  • 13
  • Why are you trying that? What are you trying to acheive? –  Dec 01 '15 at 23:36
  • How to draw an editText in a canvas http://stackoverflow.com/a/18621517/3155563 – Han Dec 01 '15 at 23:37
  • Are you giving MainMenu an explicit size? If so, check that it's large enough to show the EditText (it doesn't know anything about how big the EditText is, remember). If not, you probably need to do that! – stkent Dec 01 '15 at 23:39
  • Han, I already looked at that post, followed it, but there is no change. – user3858843 Dec 01 '15 at 23:42
  • stkent, there is no explicit size, size varies depending on the emulator's screen. The coordinates of left being equal to 200 and top equal to 500 is a location that should work. Doing a canvas.drawText() call at this location draws the text there. – user3858843 Dec 01 '15 at 23:44
  • Jawad, I would like to add a purchase feature to the app where the user can enter in the amount(quantity) of the specific item they want to purchase. The app is coded almost entirely with Java, so sticking with Java for this editText problem is the preferred solution. – user3858843 Dec 01 '15 at 23:46
  • you have to use drawing cache... this link explains about how to draw a view in a bimap. So you can use that bitmap to draw in a canvas..http://developer.android.com/reference/android/view/View.html#setDrawingCacheEnabled(boolean) – RajSharma Dec 01 '15 at 23:48
  • Please refer to this link to have an answer: http://stackoverflow.com/questions/7048103/android-drawing-button-to-canvas-with-custom-view – mdtuyen Dec 02 '15 at 02:27

0 Answers0