Skip to content Skip to sidebar Skip to footer

Use Edittext On Canvas

I'm developing an app where I need to draw a EditText on my canvas. I was able to do this, but I can just see it, not use it. Here is how I've initialized the EditText et = new Edi

Solution 1:

set EditText InputType like this :

ed.setInputType(2);

or Check Full code based on this :

LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);


    textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout1);

        finalEditTexted=newEditText(this);

        ed.setInputType(2);

        ed.setLayoutParams(lparams);

        textFieldsLayout.addView(ed);       

Solution 2:

This is not a perfect solution.But i hope it gives you some idea :)

/** Called when the activity is first created. */static Bitmap bmp;
static EditText et;
static ImageView iv;
static Canvas ivCanvas; // We'll be using our own Canvas.@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditTextet= (EditText) findViewById(R.id.editText1);
    ImageViewiv= (ImageView) findViewById(R.id.imageView1);

    // Move this up to onCreateBitmapab= BitmapFactory.decodeResource(getResources(),(R.drawable.ger)) ;
    bmp = convertToMutable(ab); // Initialize it here with the contents of ab. This effectively clones it and makes it mutable.
    ab = null; // Dispose of ab.

    ivCanvas = newCanvas(bmp); // Create our Canvas!// Add a TextWatcher
    et.addTextChangedListener(newTextWatcher() {
        publicvoidonTextChanged(CharSequence s, int start, int before, int count) {
            updateCanvas(); // Call the canvas update
        }
        publicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        publicvoidafterTextChanged(Editable s) {
        }
    });
}
publicvoidupdateCanvas() {
    ivCanvas.drawColor (Color.BLACK) ;

    ivCanvas.drawBitmap ( bmp , 0 , 0 , null ) ;

    Paintpaint=newPaint();
    paint.setColor(Color.WHITE);
    ivCanvas.drawText(et.getText().toString(),10,10,paint);

    // Everything has been drawn to bmp, so we can set that here, now.
    iv.setImageBitmap(bmp);

    // Removed the "catch" blocks so you can actually know when you're getting errors! Feel free to re-add later.
}

Solution 3:

You might use this code, in main add this method to get input keys:

@OverridepublicbooleandispatchKeyEvent(KeyEvent event) {
    cv = (CustomView) findViewById(R.id.custom_view);
    cv.dispatchKeyEvent(event);
    returnsuper.dispatchKeyEvent(event);
}

in class which extends view class:

@OverrideprotectedvoidonDraw(Canvas canvas){
    super.onDraw(canvas);

    Paintp=newPaint();
    p.setTextSize(36);

    LinearLayoutlayout=newLinearLayout(this.getContext());

    EditTexttextView=newEditText(this.getContext());
    textView.setVisibility(View.VISIBLE);
    textView.setText(key);
    textView.setX(x);//get x from onTouch method
    textView.setY(y); // get y from onTouch method

    layout.addView(textView);

    layout.measure(canvas.getWidth(), canvas.getHeight());
    layout.layout(50,50, canvas.getWidth(), canvas.getHeight());
    layout.draw(canvas);
} 


 @OverridepublicbooleandispatchKeyEvent(KeyEvent event) {
    intkeyaction= event.getAction();

    if(keyaction == event.ACTION_DOWN)
    {
        intkeyunicode= event.getUnicodeChar(event.getMetaState() );
        charcharacter= (char) keyunicode;
        key += character;
        System.out.println("DEBUG MESSAGE KEY=" + character);
    }
    // you might add if (delete) // https://stackoverflow.com/questions/7438612/how-to-remove-the-last-character-from-a-string// method to delete last character 
    invalidate();
    returnsuper.dispatchKeyEvent(event);
}

You will need to store the text in an object, this object contains x, y, width, height, string

in the onTouch method, check if the click is in the same text, then make new EditText >> false and make the old key to be modified.

Post a Comment for "Use Edittext On Canvas"