I am trying to display a rectangle when i tap on my android phone screen after I run the android project. This is what I have tried so far . Could anyone give an idea what to use in onTouchListener method to actually make a rectangle display when i tap ??
package example.rectangle; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.*; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import java.util.Timer; import java.util.TimerTask; public class RectView extends View { ShapeDrawable rect ; public Rect rect1 ; public int argbColour ; public RectView(Context context , AttributeSet attrs) { super(context , attrs); initRectangle(); } public RectView (Context context , AttributeSet attrs , int defStyle) { super(context , attrs , defStyle); initRectangle(); } private void initRectangle() { rect = new ShapeDrawable(new RectShape()); rect.getPaint().setColor(0xffff0000); } //Draws the rectangle on canvas with a ShapeDrawable object public void onDraw(Canvas canvas) { super.onDraw(canvas); rect.setBounds(250, 25,450, 600); rect.draw(canvas); } //view.setOnTouchListener(new TouchListener()); public class TouchListener implements View.OnTouchListener { public boolean onTouch(View v, MotionEvent e) { // TODO Auto-generated method stub if (v.getId()==R.id.rect ) { // how to draw rectangle while the user tap on screen??????? } return true; } } } // *******************Another activity class***************** package example.rectangle; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class RectActivity extends Activity { private RectView rectView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rect); rectView = (RectView)findViewById(R.id.rect); if (savedInstanceState != null) { // activity being restored from earlier launch } } }