Question

i want to place edit text on tap on canvas. i want to take input from user and set that edit text to canvas.

here is my code:

@Override
protected void onDraw(Canvas canvas) {


    for (Point point : points) {

        LinearLayout lL = new LinearLayout(getContext());

           EditText editTextView = new EditText(getContext()); 

           editTextView.setFocusableInTouchMode(true);
           editTextView.setFocusable(false);
           editTextView.setClickable(true);

           editTextView.setText("sadsdadadadadsadasds");
           editTextView.setVisibility(View.VISIBLE);
           lL.addView(editTextView);
           lL.setDrawingCacheEnabled(true);

            lL.measure(canvas.getWidth(), canvas.getHeight());
            lL.layout(0, 0, canvas.getWidth(), canvas.getHeight());

            canvas.drawBitmap(lL.getDrawingCache(),  point.x, point.y, mPaint);
     //   canvas.drawText("(0,0)", point.x, point.y, mPaint);




      //  Log.d(TAG, "Painting: "+point);
    }
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    InputMethodManager inputMethodManager;
    TextView tv1=new TextView(getContext());
    LinearLayout lL = new LinearLayout(getContext());

    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = (int) event.getX();
    point.y = (int) event.getY();
    points.add(point);
    invalidate();
    return true;
}

Error:

PROBLEM is that EditText is Getting placed on Canvas but not clickble

Was it helpful?

Solution

First dont draw it on canvas, as touches are applied only to that canvas, in your Activity/xml draw the edit text and your custom view (canvas), if still not clickable try to bring edit text to front

yourLayout.bringChildToFront(yourEditText);

your xml be something like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- your custom view here -->


    <!-- edittext here -->
    <EditText
        android:id="@+id/settings_text_y"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

OTHER TIPS

This code worked for me. Please let me know it doesn't:

final RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,      LayoutParams.MATCH_PARENT));
setContentView(mainLayout);
mainLayout.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN) {
        return false;
    }

    EditText editText = new EditText(YourActivity.this);

    // Add whatever you want as size
    int editTextHeight = 70;
    int editTextWidth = 200;

    LayoutParams params = new LayoutParams(editTextWidth, editTextHeight);

    //You could adjust the position
    params.topMargin = (int) (event.getRawY());
    params.leftMargin = (int) (event.getRawX());
    mainLayout.addView(editText, params);
    editText.requestFocus();

    return false;
   }
 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top