You might use this code, in main
add this method to get input keys:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
cv = (CustomView) findViewById(R.id.custom_view);
cv.dispatchKeyEvent(event);
return super.dispatchKeyEvent(event);
}
in class which extends view class:
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint p = new Paint();
p.setTextSize(36);
LinearLayout layout = new LinearLayout(this.getContext());
EditText textView = new EditText(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);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyaction = event.getAction();
if(keyaction == event.ACTION_DOWN)
{
int keyunicode = event.getUnicodeChar(event.getMetaState() );
char character = (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();
return super.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.