Ruby
Ruby

Reputation: 782

How to create Clickable Rectangles?

Im drawing an indoor map using DrawRect method. I want to identify each rectangle by the user's OnTouch Event.How can i make it happen?

I want to know if i can use the drawn Rectangles as independent objects Where i can identify them uniquely.

UPDATED

I tried this. But it gives me errors.How can i create a new intent withing the for loop?? :(

ArrayList<Rect> rectangles = new ArrayList<Rect>();
 {rectangles.add(new Rect(232,78,419,238));
 rectangles.add(new Rect(285,27,524,239));
 rectangles.add(new Rect(418,79,524,239));
 rectangles.add(new Rect(181,79,233,215));
 rectangles.add(new Rect(232,237,524,315));}

 String Selected_rect = null;


public boolean onTouchEvent(MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
switch(event.getAction()){

case MotionEvent.ACTION_DOWN:
        System.out.println("Touching down!");
        for(int i =0; i< rectangles.size();i++){

                if(rectangles.get(i).contains(touchX,touchY)){
                    System.out.println("Touched Rectangle, start activity.");
                    rectangles.get(i).describeContents ();
                    Selected_rect = String.valueOf(rectangles.get(i));
                                       }
//                    Intent inte = new Intent("android.intent.action.DetectBlock");
//                    startActivity(inte);
            }

        break;
case MotionEvent.ACTION_UP:
        System.out.println("Touching up!");
        break;
case MotionEvent.ACTION_MOVE:
        System.out.println("Sliding your finger around on the screen.");
        break;
}
  return true;
}

My Intent method body

private void startActivity(Intent inte) {
    // TODO Auto-generated method stub
     startActivity(inte);
}

Upvotes: 0

Views: 1560

Answers (3)

Winz Gagnier
Winz Gagnier

Reputation: 1

Im not sure if this has been answered but i am building something similar. For the construction of the rects i have just manually added each and then used an Rect.intersect() to see if the constructed rects cross the touchpoint. Thats a vague answer but i figure you figured it out, let me know if you didn't:D

Upvotes: 0

Seperot
Seperot

Reputation: 23

I realize this question was asked a while back but i noticed your updated question...

I'm guessing the problem your having is actually your trying to run "startActivity" from a View class.

Firstly if you make a new static void on your activity class like so

public static void Rectclicked(Context c) {
        Intent inte = new Intent(c, SecondActivity.class);
        c.startActivity(inte);
    }

then back on your View class, in your TouchEvent add this into the for loop if a rectangle has been clicked

MainActivity.Rectclicked(getContext());

Upvotes: 0

ixx
ixx

Reputation: 32273

The canvas will not remember about the shapes you draw on it. You have to keep track of them separatedly. You should fill a list (or some other data structure) with Rect objects ("model" objects), or other custom objects holding the coordinates, dimensions and whatever you need, of the rectangles, and check on touch if the coordinates of the touch are inside of any of these rectangles. E.G. method contains(int x, int y) of Android's Rect will help. In your draw method you then use also these objects.

Upvotes: 1

Related Questions