1

I've tried so many ways to show the Toast I want (on the DrawView Class) that I really don't know where to run anymore.

Apparently this is really simple, but I'm missing something. I've seen other related posts but had no success.

Any ideas please?

import android.app.Activity;
import android.os.Bundle;


public class MyActivity extends Activity{

    DrawView drawView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        setContentView(drawView);

    }

}

DrawView Class:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


public class DrawView extends View implements View.OnTouchListener{

    Paint paint = new Paint();

    public DrawView(MyActivity myActivity) {
        super(myActivity);

        setBackgroundColor(Color.WHITE);
        paint.setColor(Color.BLACK);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawText("I'm text in a canvas!", 10, 10, paint);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN){

            Toast.makeText(getContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();

        }

        return false;

    }

}
Danilo Setton
  • 601
  • 10
  • 20
  • Have a look at this [link](http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context/13945633#13945633). – ChuongPham Jan 21 '15 at 17:46

5 Answers5

4
public class DrawView extends View  {

Paint paint = new Paint();
private Context context;

public DrawView(MainActivity context) {
    super(context);
    this.context = context;
    setBackgroundColor(Color.WHITE);
    paint.setColor(Color.BLACK);
}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawText("I'm text in a canvas!", 10, 10, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        Toast.makeText(context, "I'm a Toast!", Toast.LENGTH_SHORT).show();

    }

    return false;

}}
Dardan
  • 531
  • 7
  • 15
1

Change the code as follows..

Context _context;
public DrawView(MyActivity myActivity,Context context) {
        super(myActivity);

        _context=context;
        setBackgroundColor(Color.WHITE);
        paint.setColor(Color.BLACK);

    }

and show Toast using _context

Toast.makeText(_context,"I'm a Toast!",Toast.LENGTH_SHORT).show();

Pass the current Context also as an argument to the class..

Lal
  • 14,726
  • 4
  • 45
  • 70
1

The Toast not apper is not the context reference issue its is with you on touch event problem. You Should change your DrawView class like this

public class DrawView extends View  {

Paint paint = new Paint();
Context _MyActivity;

public DrawView(Context myActivity) {
    super(myActivity);
    this._MyActivity = myActivity;
    setBackgroundColor(Color.GREEN);
    paint.setColor(Color.BLACK);

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawText("I'm text in a canvas!", 10, 10, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // do something
        Toast.makeText(_MyActivity, "I'm a Toast!", Toast.LENGTH_SHORT)
        .show();
        break;
    case MotionEvent.ACTION_MOVE:
        // do something
        break;
    case MotionEvent.ACTION_UP:
       //do something
        break;
}
return true;
}



}

and your Your activity class like this

public class MyActivity extends Activity implements OnTouchListener {

DrawView drawView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     drawView = new DrawView(this);
        setContentView(drawView);
        drawView.setOnTouchListener(this);
}



@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}

}

You dont want implement OnTouchListener in DrawView.class

Hope this will help you

Ramz
  • 7,116
  • 6
  • 63
  • 88
0

You can try to change your code from:

Toast.makeText(getContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();

to:

Toast.makeText(getApplicationContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
0

You are passing in the Activity use that as your context.

gsueagle2008
  • 4,583
  • 10
  • 36
  • 46