1

I place an ImageView of a pin in the center of the layout by using android:layout_centerInParent="true" in my RelativaLayout XML file.

enter image description here

Now I wish to draw the green dot at the same position as the pin on the canvas.

NOTE: the green dot is NOT a view. It is drawn on canvas by canvas.drawCircle();

That is, I have to programmatically get the coordinates of the pin.

So how can I get the coordinates of android:layout_centerInParent="true" with codes?

Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174

4 Answers4

2

My guess is

layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);

You can get layoutParams by relativeLayout.getLayoutParams(), and don't forget to setLayoutParams back when you're done modifying it.

josephus
  • 8,284
  • 1
  • 37
  • 57
2

To get the width and height of the parent you can do this.

RelativeLayout parent = (RelativeLayout) findViewById(R.id.yourRelativeLayout);
int width = parent.getWidth();
int height = parent.getHeight();

Then you can divide these numbers by 2 and set that to as your green dot's coordinates and it should appear in the middle of your screen. For this to work your canvas size has to be the same as the relative layout.

But beware, you need to call getWidth() and getHeight() methods after the activity has been created, else you will end up getting zero. See this answer

Community
  • 1
  • 1
Mazvél
  • 951
  • 1
  • 8
  • 22
  • _For this to work your canvas size has to be the same as the relative layout._ Here comes my problem! Since my canvas contains a picture, the canvas size is NOT the same as the layout. You can also see the buttons there. They are NOT on canvas. So the canvas size is different from the layout size. So how? – Sibbs Gambling Jul 15 '13 at 02:13
  • the `parent` is `null` when run. `NullPointerException`. Why? – Sibbs Gambling Jul 15 '13 at 02:43
  • oh, sorry, I mis-corrected your codes. u r right, it should be `R.id`. But why in my `R.id` I only have buttons, textviews, but NO layout? – Sibbs Gambling Jul 15 '13 at 02:51
1

So, the canvas you are drawing to is in a view that is contained by the RelativeLayout, but you want to draw the dot at the center of the RelativeLayout?

Yes, that is exactly what I am trying to do!

Assuming the canvas view is a direct child of the RealtiveLayout, this should work.

You can get the layout's center by using getWidth() / 2 and getHeight() / 2 on the layout as others mentioned. However, you also have to figure out where the origin of the canvas is. For this you can just use getLeft() and getTop() on the canvas view. Then you just subtract the center x from left, and center y from top to get your final spot.

Example:

grid

Assume each grid line is 1. The RelativeLayout is the large black rectangle, and the Canvas view is the blue one. The center dot's coordinates are 4,6. Using left/top, you get 1,4 for the canvas origin(red dot). Subtract, and you get 3,2, which are the local canvas coordinates for the green dot.

Community
  • 1
  • 1
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • What you have said are absolutely correct. This is also what I am doing. But I have another problem when I try to get the dimensions of the layout as @Mazvél suggests. Please kindly help! http://stackoverflow.com/questions/17646188/no-relativelayout-id-in-r-id-while-getting-dimensions-of-relativelayout – Sibbs Gambling Jul 15 '13 at 03:17
0

Drawing the dot in the center of the canvas should just be a matter of dividing the width and height of the view by two. If you want it to be at the base of the pin (as opposed to the true center), then just add half the height of the pin to the circle's y value.

nEx.Software
  • 6,782
  • 1
  • 26
  • 34
  • No. I need the dot to be at the pin location, which is NOT the center of the canvas. It is the center of the view. – Sibbs Gambling Jul 15 '13 at 02:09
  • Center of canvas and center of view are usually the same. Anyway, dividing the width and height of the view as I said is the center of the view. – nEx.Software Jul 15 '13 at 02:12
  • my canvas contains a picture. Also, you can see the 4 buttons below. They are not on the canvas. So the canvas size is different from the layout size. – Sibbs Gambling Jul 15 '13 at 02:14
  • yeah! u r right on _Center of canvas and center of view are usually the same._ But I wish to draw the dot at the center of the LAYOUT instead of the VIEW. :) – Sibbs Gambling Jul 15 '13 at 02:17
  • So, the canvas you are drawing to is in a view that is contained by the RelativeLayout, but you want to draw the dot at the center of the RelativeLayout? This makes no sense. If that is the case, then you should be overriding the drawing of the layout and not the view. – nEx.Software Jul 15 '13 at 02:28
  • Yes, that is exactly what I am trying to do! What do you mean by _you should be overriding the drawing of the layout and not the view._ ? – Sibbs Gambling Jul 15 '13 at 02:32
  • I mean override the RelativeLayout's dispatchDraw method to add your circle to the canvas. – nEx.Software Jul 15 '13 at 02:36