1

I have a button that create a text view at a coordinate in a relative layout, what I want to do is check to see if a text view is already created at that position so that i can create the text view somewhere else.

To sum it up, my question is how to check to see if another view is at location x and y? Here is the creation code:

@Override
   public void onClick(View v) {
   DragTextView textview = MainActivity.createTextView(context);
   textview.setX(getX()+getWidth()+30);
   textview.setY(getY()+getHeight()+30);
}
Sam
  • 7,252
  • 16
  • 46
  • 65
  • 1
    If you are always setting the position on your own, then keep a `List` or `HashMap` to keep track of positions of views. Then when drawing another view, check against this `List` or `HashMap` to see if there is already another view at that position.. – Amulya Khare Nov 22 '13 at 10:52
  • Does Android have a build-in function for this kind of check? because manually checking the position and boundary of each view is quite a tedious task for me. – Loyally Lion Nov 22 '13 at 11:00

2 Answers2

1

this sample code may help you...

    int left = textView.getLeft();
    int right = textView.getRight();
    int top = textView.getTop();
    int bottom = textView.getBottom();
    Rect rect = new Rect(left, top, right, bottom);
    int x = 200;
    int y = 250;
    if(rect.contains(x, y)) {
        Log.i(TAG, "TextView occupied (200,250) position");
    }
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • Thank you, I didn't know of the Rect class, I think i'm going to use a for loop for all of the child in the relative layout and check each one's Rect to see if any of them intersect. – Loyally Lion Nov 22 '13 at 11:07
0

See if this can help you. I get this solutions are providing the coordinate of views. Store this values in some Collection object and refer same before placing the view

Getting View's coordinates relative to the root layout

How to get screen cordinates corresponding to the whole screen, and not the View touched?

Community
  • 1
  • 1
Vaibs
  • 1,128
  • 3
  • 16
  • 36
  • I think getX() and Y fits the purpose already since i need the coordinate according to the parent layout and not the screen, I'm using a 2d ScrollView, which means that the layout can extend much further than the screen (about 2000 in width and height) – Loyally Lion Nov 22 '13 at 11:03