Wale Bear
Wale Bear

Reputation: 25

OnTouch doesnt work where do I have to put the onTouchlistener

Hello so I have got a main class whic sets up a new View its the painting class i made and in it I have got the ondraw method which draws a Rectangle from another class. I want to be able to move the rectangle by touching on the screen so i would use the ontouchlistener but it doesnt seem to work wherever I click on the screen or drag my mouse on the emulator the rectangle doesnt move How can I correct that?

 // Painting class 
public class Painting extends View implements OnTouchListener{
    myRectangle player = new myRectangle();
    public Painting(Context context) {
        super(context);

    // TODO Auto-generated constructor stub
    DisplayMetrics dm = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
    player.size = (int) (dm.widthPixels / 7.5);
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    player.drawBlock(canvas);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    player.xpos = (int) event.getX();
    player.ypos = (int) event.getY();
    return false;
}

}

My Main class:

public class Main extends Activity {
Painting v;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    v = new Painting(Main.this);
    v.setOnTouchListener((OnTouchListener) Main.this);
    setContentView(v);

}

and this is my Rectangle class:

public class myRectangle{
public int size;
public int xpos;
public double ypos;
public int instrY;
private Paint paint = new Paint();
public myRectangle(){
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.parseColor("#34495E"));
}

public void drawBlock(Canvas c){
    c.drawRect(xpos, (float) ypos, size, size, paint);
}

}

Upvotes: 0

Views: 107

Answers (2)

Jickson
Jickson

Reputation: 5193

Add "setOnTouchListener(this);" inside Painting class.

Like below code,

// Painting class 
public class Painting extends View implements OnTouchListener{
    myRectangle player = new Rectangle();
    public Painting(Context context) {
        super(context);

    // TODO Auto-generated constructor stub
    DisplayMetrics dm = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
    player.size = (int) (dm.widthPixels / 7.5);
    setOnTouchListener(this);
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    player.drawBlock(canvas);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    player.xpos = (int) event.getX();
    player.ypos = (int) event.getY();
    return false;
}

and remove the line "v.setOnTouchListener((OnTouchListener) Main.this);" from activity class.

Upvotes: 0

barq
barq

Reputation: 3711

You are not associating the OnTouchListener you are implementing in your onCreate(). You are referring to some OnTouchListener of Main. Therefore you are not executing the onTouch method implemented in Painting.

Upvotes: 1

Related Questions