Madhu Sudhan
Madhu Sudhan

Reputation: 31

How to create a pattern Lock app

I am new to android, In my app I just need to add a screen like pattern lock, what exactly I want to do is, no need to create/ set a pattern.Just design a screen with 9 dots, and when user draw some pattern and I need to get those pattern value.For design I used PatternView but how can I get the user drawer pattern? I already google on this, but I found there they setting the pattern lock, I don't want that.I just want is when I draw something on PatternView it just return the value like,suppose like if I draw a pattern like 2365 dots it will return an integer value like 2365.

I already tried haibison.github.io/android-lockpattern but my app is different than this.I just want to draw a pattern and get result, no need to create and confirm screens. No need to save the result in shared preferences. I just want compare the drawn pattern value with my predefined value.

Upvotes: 2

Views: 2230

Answers (1)

Rahul Tiwari
Rahul Tiwari

Reputation: 6968

Following is idea on how to go for this:

  1. Start with a table view for your layout and create a 3 x 3 grid of image views (dots).
  2. override onTouchListener on your activity to detect touches on images. refer this answer for details.
private ImageView imageView;
private Rect imageRect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView1);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    if (imageRect == null) {
        imageRect = new Rect();
        imageView.getGlobalVisibleRect(imageRect);
    }
    int x = (int) event.getX();
    int y = (int) event.getY();
    if (imageRect.contains(x, y)) {
        Log.i(TAG, "touch passing over imageView");
    }
    return true;
}

example is for 1 image view you have to build it for 9.

  1. as and when touch is detected append the index of imageview to a string.

  2. once an image is touched stop detecting touches on it.

  3. perform desired operation when MotionEvent.ACTION_UP is detected

Upvotes: 1

Related Questions