0

im trying to create a toast with a message that displays the coordinates depending where the user clicks on the screen. when i declare 'mContext=this' i get an error: incompatible types required: Context.

@Override public boolean onTouchEvent(MotionEvent event) { //motionevent detects motion from the user float x; x = event.getX(); float y; y = event.getY(); Context mContext; switch (event.getAction()) { case MotionEvent.ACTION_UP:

//touch_up(x, y);

mContext= this;

            float Cox = event.getRawX();
            float Coy = event.getRawY();
            String text = "You clicked at x = " + Cox+ "and y =" + Coy;
            //AlertDialog.Builder builder = new AlertDialog.Builder();
            Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
            //toast.show();
            invalidate();
            break;
      }
      return true;

    }

1 Answers1

0

Is this within an Activity or Fragment? this will work in an Activity, however in a Fragment you will need to call the context of the host Activity.

Something like: mContext = getActivity(); Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();

Shazbot
  • 1,444
  • 1
  • 11
  • 21