1

I am trying to do something similar to that of the android lock screen pattern. I have a class that extends a view that I create multiple instances of. These appear on the screen all at once.

I need to be able to click on them individually and have each one turn green individually, however only one on touch listener is listening at once and it belongs to the last dot which appeared, so if I click anywhere on the screen the last appeared dot turns green no matter where I click.

Here is the code for my dot class:

package com.ewebapps;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Dot extends View implements OnTouchListener {
     private final float x;
     private final float y;
     private final int r;
     private final Paint mBlack = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mGreen = new Paint(Paint.ANTI_ALIAS_FLAG);
     private boolean touched;
/*     
     public boolean onInterceptTouchEvent(View v, MotionEvent event) {
   touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      invalidate();
   return true;
  }
*/
     public Dot(Context context, float x, float y, int r) {
         super(context);
         mBlack.setColor(0xFF000000); //Black
         mWhite.setColor(0xFFFFFFFF); //White
         mGreen.setColor(0xFF00FF00); //Green
         this.x = x;
         this.y = y;
         this.r = r;
         this.setOnTouchListener(new OnTouchListener(){

                @Override

                public boolean onTouch(View v, MotionEvent event) {

                 touched = true;
                 //mPaint.setColor(0xFF00FF00); // Turn dot green.
              v.invalidate();  
              return true;

                }
        });

     }

 /*    @Override
  public boolean dispatchTouchEvent(MotionEvent event) { // On touch.
      touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      this.invalidate();
         return super.dispatchTouchEvent(event);
     }
  */
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         canvas.drawCircle(x, y, r+2, mWhite); //White stroke.

         if(!touched)
         {
          canvas.drawCircle(x, y, r, mBlack); //Black circle.
         }
         else
         {
          canvas.drawCircle(x, y, r, mGreen); //Green circle.
         }
     }

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



/* @Override
 public boolean onTouch(View v, MotionEvent event) {
   touched = true;
     //mPaint.setColor(0xFF00FF00); // Turn dot green.
     invalidate();
     return true;
 }
*/
}

And here is my main class:

package com.ewebapps;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;


public class dots extends Activity{

 public Boolean isRunning, isPaused;
 public int maxdots = 20;
 public int currentdotdraw = 1;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    public void newdotdraw(){

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int screenwidth = display.getWidth();
    int screenheight = display.getHeight();

    int x = (int) (Math.random() * screenwidth) + 1; 
    int y = (int) (Math.random() * screenheight) + 1;  

    FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
    main.addView(new Dot(this,x,y,25));

    }

}

In the code I called newdotdraw multiple times.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Eweb
  • 71
  • 1
  • 12
  • My guess is the dot is trying to fill the whole screen, so it believes a press anywhere on it constitutes a press on itself, and because of layering it's the only pressed dot. I'm guessing that would be a problem with your dots' canvases or views. – Joshua Patton Nov 09 '10 at 02:45
  • How do you suggest I go about limiting the size of my dot classes view? Thanks! – Eweb Nov 09 '10 at 05:24

1 Answers1

0

Could you add a transparent custom view on top (overlay) & then track the touch events and see to which button they belong. (simple math).

That would make it easier for you to draw lines (similar to the unlock pattern), too.

Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
  • That sounds like a working solution, unfortunately I am very new to the Android scene and do not know how to do that, would you be able to give me an example of how to either do that or limit the size of my view so it does not fill the whole screen? It would be much appreciated. Thanks! – Eweb Nov 09 '10 at 05:23